Module: AssociatedScope::ParentClassMethods1
- Defined in:
- lib/associated_scope.rb
Instance Method Summary collapse
- #associated_methods(&block) ⇒ Object
- #associated_scope(name, scope, source:) ⇒ Object
- #associated_scope1(name, scope, source:) ⇒ Object
- #associated_scope_args ⇒ Object
- #extended(base) ⇒ Object
Instance Method Details
#associated_methods(&block) ⇒ Object
89 90 91 |
# File 'lib/associated_scope.rb', line 89 def associated_methods(&block) @associated_methods = block end |
#associated_scope(name, scope, source:) ⇒ Object
85 86 87 |
# File 'lib/associated_scope.rb', line 85 def associated_scope(name, scope, source:) associated_scope_args[name] = { scope: scope, source: source } end |
#associated_scope1(name, scope, source:) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/associated_scope.rb', line 93 def associated_scope1(name, scope, source:) reflection = _reflect_on_association(source) raise AssociationNotFoundError.new(self, source) unless reflection reflection = reflection.dup parent_scope = reflection.scope myscope = proc { instance_exec(&parent_scope).merge!(instance_exec(&scope)) } reflection.define_singleton_method(:name) do name end reflection.define_singleton_method(:scope) do myscope end associated_scope_args[name] = { scope: scope, reflection: reflection } define_method(name) do association(name).reader end end |
#associated_scope_args ⇒ Object
81 82 83 |
# File 'lib/associated_scope.rb', line 81 def associated_scope_args @associated_scope_args ||= {} end |
#extended(base) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/associated_scope.rb', line 23 def extended(base) klass = base.respond_to?(:klass) ? base.klass : base.class associated_scope_args2 = associated_scope_args associated_scope_args.each do |name, args| source = args[:source] reflection = klass._reflect_on_association(source) raise AssociationNotFoundError.new(self, source) unless reflection reflection = reflection.dup parent_scope = reflection.scope associated_scope_arg = args[:scope] myscope = proc do instance_exec(&parent_scope).merge!(instance_exec(&associated_scope_arg)) end reflection.define_singleton_method(:name) do name end reflection.define_singleton_method(:scope) do myscope end associated_scope_args[name].merge! reflection: reflection end associated_methods = @associated_methods myblock = proc do |record| record.define_singleton_method(:association) do |name| association = association_instance_get(name) return association unless association.nil? args = associated_scope_args2[name] return super(name) unless args reflection = args[:reflection] association = reflection.association_class.new(self, reflection) association_instance_set(name, association) association.association_scope association end associated_scope_args2.keys.each do |name| record.define_singleton_method(name) do association(name).reader end end record.instance_eval(&associated_methods) end if base.is_a?(ActiveRecord::Base) myblock.call(base) else base.extend RelationExtension base.define_singleton_method(:myblock) { myblock } end end |