This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add a module builder that'll mask each class method? | |
def self.required_condition_via(scopes) | |
prepend RequiredCondition.new(scopes) | |
end | |
module RequiredCondition | |
def initialize(scopes) | |
@scopes = scopes | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/feeds_controller.rb | |
class FeedsController < ApplicationController | |
before_action :set_feed | |
def show | |
# Not sure if I like this but there could be something similar to interactors? | |
action do | |
_1.success.html { redirect_to @feed, notice: "Success" } | |
_1.success.json | |
_1.unprocessible { render :new } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Method::Chain | |
def initialize(descriptor, **options) | |
@descriptor, @options = descriptor, options | |
end | |
def self.define(context, *methods, &block) | |
instance = new Definition.new(&block) | |
instance.apply context, *methods | |
instance | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "tsort" | |
module Rails | |
module Initializable | |
extend ActiveSupport::Concern | |
def run_initializers(group = :default, *arguments) | |
@ran ||= true.tap do | |
Initializer.tsort(initializers).each { instance_exec(*arguments, &_1.block) if _1.in?(group) } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/views/cards/_card.html.erb | |
# <%= greeting %> | |
# | |
# ^ gets compiled onto ActionView::Base as a Ruby method, when called like this: | |
# | |
# render "cards/card", greeting: "Hello" | |
# | |
# Here's roughly what the compiled source looks like: | |
class ActionView::Base | |
def render_card(local_assigns, output_buffer) # Not the actual method name, and the arguments change if using strict locals |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Invoice | |
delivers_exposition # only: :create ? | |
# ^ shorthand for this: | |
after_create_commit -> { Exposition.deliver_later "invoice.create", self } | |
after_update_commit -> { Exposition.deliver_later "invoice.update", self } | |
after_destroy_commit -> { Exposition.deliver_later "invoice.destroy", self } | |
end | |
class Exposition < Data.define(:service, :event, :gid) | |
def initialize(service: "invoices", **) = super |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Developer | |
has_object :search_scorer, after_save_commit: :rebuild_later # Forward callback onto context object. | |
end | |
# From my https://github.com/kaspth/active_record-associated_object gem. | |
# app/models/developer/search_scorer.rb | |
class Developer::SearchScorer < ActiveRecord::AssociatedObject | |
performs :rebuild # Adds `rebuild_later` to automatically generate a job to run the scoring calculation in. | |
def rebuild | |
record.update! search_score: new_score |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object::Proxy < SimpleDelegator | |
def initialize(object, **values) | |
super(object) | |
@values = values | |
end | |
def method_missing(name, ...) | |
@values.fetch(name) { super } | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Provide a BulletTrain::Theme::FormBuilder, with extensions to | |
# go between Rails standard form helpers and BulletTrain's themed versions seamlessly. | |
# With standard Rails: | |
# form_with model: Post.new do |form| | |
# form.themed.text_field # Use BulletTrain theming on just one field | |
# | |
# form.themed.fields do # Need a nested section of fields but themed? | |
# end | |
# end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FormBuilder | |
def text_field(method, **options) | |
tag.input type: :text, **options_for(__method__, method, **options) | |
end | |
def options_for(type, method, index: @index, namespace: @options[:namespace], multiple: false, **options) | |
Options.new(self, type, method, index:, namespace:, multiple:, **options) | |
end | |
class Options |