Class: RailsAdmin::Adapters::ActiveRecord::Association
- Inherits:
-
Object
- Object
- RailsAdmin::Adapters::ActiveRecord::Association
- Defined in:
- lib/rails_admin/adapters/active_record/association.rb
Instance Attribute Summary collapse
-
#association ⇒ Object
readonly
Returns the value of attribute association.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #as ⇒ Object
- #association? ⇒ Boolean
- #field_type ⇒ Object
- #foreign_inverse_of ⇒ Object
- #foreign_key ⇒ Object
- #foreign_key_nullable? ⇒ Boolean
- #foreign_type ⇒ Object
-
#initialize(association, model) ⇒ Association
constructor
A new instance of Association.
- #inverse_of ⇒ Object
- #key_accessor ⇒ Object
- #klass ⇒ Object
- #name ⇒ Object
- #nested_options ⇒ Object
- #polymorphic? ⇒ Boolean
- #pretty_name ⇒ Object
- #primary_key ⇒ Object
- #read_only? ⇒ Boolean
- #type ⇒ Object
Constructor Details
#initialize(association, model) ⇒ Association
Returns a new instance of Association.
9 10 11 12 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 9 def initialize(association, model) @association = association @model = model end |
Instance Attribute Details
#association ⇒ Object (readonly)
Returns the value of attribute association.
7 8 9 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 7 def association @association end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
7 8 9 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 7 def model @model end |
Instance Method Details
#as ⇒ Object
99 100 101 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 99 def as [:as].try :to_sym end |
#association? ⇒ Boolean
121 122 123 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 121 def association? true end |
#field_type ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 26 def field_type if polymorphic? :polymorphic_association else :"#{association.macro}_association" end end |
#foreign_inverse_of ⇒ Object
80 81 82 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 80 def foreign_inverse_of nil end |
#foreign_key ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 60 def foreign_key if association.[:query_constraints].present? association.[:query_constraints].map(&:to_sym) elsif association.foreign_key.is_a?(Array) association.foreign_key.map(&:to_sym) else association.foreign_key.to_sym end end |
#foreign_key_nullable? ⇒ Boolean
70 71 72 73 74 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 70 def foreign_key_nullable? return true if foreign_key.nil? || type != :has_many (column = klass.columns_hash[foreign_key.to_s]).nil? || column.null end |
#foreign_type ⇒ Object
76 77 78 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 76 def foreign_type [:foreign_type].try(:to_sym) || :"#{name}_type" if [:polymorphic] end |
#inverse_of ⇒ Object
107 108 109 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 107 def inverse_of [:inverse_of].try :to_sym end |
#key_accessor ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 84 def key_accessor case type when :has_many, :has_and_belongs_to_many :"#{name.to_s.singularize}_ids" when :has_one :"#{name}_id" else if foreign_key.is_a?(Array) :"#{name}_id" else foreign_key end end end |
#klass ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 34 def klass if [:polymorphic] polymorphic_parents(:active_record, association.active_record.name.to_s, name) || [] else association.klass end end |
#name ⇒ Object
14 15 16 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 14 def name association.name.to_sym end |
#nested_options ⇒ Object
117 118 119 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 117 def model..try { |o| o[name.to_sym] } end |
#polymorphic? ⇒ Boolean
103 104 105 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 103 def polymorphic? [:polymorphic] || false end |
#pretty_name ⇒ Object
18 19 20 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 18 def pretty_name name.to_s.tr('_', ' ').capitalize end |
#primary_key ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 42 def primary_key return nil if polymorphic? value = case type when :has_one association.klass.primary_key else association.association_primary_key end if value.is_a? Array :id else value.to_sym end end |
#read_only? ⇒ Boolean
111 112 113 114 115 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 111 def read_only? (klass.all.instance_exec(&scope).readonly_value if scope.is_a?(Proc) && scope.arity == 0) || association.nested? || false end |
#type ⇒ Object
22 23 24 |
# File 'lib/rails_admin/adapters/active_record/association.rb', line 22 def type association.macro end |