Module: DelegateAssociations
- Defined in:
- lib/delegate_associations.rb,
lib/delegate_associations/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Method Summary collapse
Instance Method Details
#delegate_associations(*opts) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/delegate_associations.rb', line 7 def delegate_associations(*opts) = { except: [], only: [], allow_nil: false, to: [] } .update(opts.) associations = [.delete(:to)].flatten.compact.map!(&:to_sym) valid_associations_to(associations) except = [[:except]].flatten.compact.map!(&:to_sym) only = [[:only]].flatten.compact.map!(&:to_sym) except += delegate_exclude_columns associations.each do |association| reflect_on_association(association).klass.reflect_on_all_associations.each do |ass| next unless ass.name.in?(get_deletage_methods(reflect_on_association(association).klass.reflect_on_all_associations.map(&:name), except, only)) delegate "#{ass.name}", to: association, allow_nil: [:allow_nil] delegate "#{ass.name}=", to: association, allow_nil: [:allow_nil] begin delegate "#{ass.name}_attributes=", to: association, allow_nil: [:allow_nil] rescue true end unless ass.collection? delegate "build_#{ass.name}", to: association end end end end |
#delegate_attributes(*opts) ⇒ Object
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 |
# File 'lib/delegate_associations.rb', line 37 def delegate_attributes(*opts) = { suffix: ["","=","?","_before_type_cast","_change","_changed?","_was","_will_change!"], except: [], only: [], allow_nil: false, to: [] } .update(opts.) associations = [.delete(:to)].flatten.compact.map!(&:to_sym) #Valid if have an option[:to] and if association exists valid_associations_to(associations) except = [[:except]].flatten.compact.map!(&:to_sym) only = [[:only]].flatten.compact.map!(&:to_sym) except += delegate_exclude_columns # I need "begin" because have a problem with Devise when I run migrations # Devise call User classs before run all migrations begin associations.each do |association| get_deletage_methods(reflect_on_association(association).klass.column_names, except, only).each do |attribute| [:suffix].each do |sf| delegate "#{attribute}#{sf}", to: association, allow_nil: [:allow_nil] end end end rescue true end end |