Class: AdminData::ActiveRecordUtil
- Inherits:
-
Object
- Object
- AdminData::ActiveRecordUtil
- Defined in:
- lib/admin_data/active_record_util.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
Returns the value of attribute klass.
Instance Method Summary collapse
-
#declared_belongs_to_association_names ⇒ Object
class User belongs_to :club end.
-
#declared_habtm_association_names ⇒ Object
class User has_and_belongs_to_many :clubs end.
-
#declared_has_many_association_names ⇒ Object
class User has_many :cars end.
-
#declared_has_one_association_names ⇒ Object
class User has_one :car end.
-
#habtm_klasses_for ⇒ Object
returns an array of classes.
-
#has_association_info? ⇒ Boolean
returns false if the given Klass has no association info.
-
#initialize(klass) ⇒ ActiveRecordUtil
constructor
A new instance of ActiveRecordUtil.
-
#klass_for_association_type_and_name(association_type, association_name) ⇒ Object
returns a class or nil.
Constructor Details
#initialize(klass) ⇒ ActiveRecordUtil
Returns a new instance of ActiveRecordUtil.
6 7 8 |
# File 'lib/admin_data/active_record_util.rb', line 6 def initialize(klass) @klass = klass end |
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
4 5 6 |
# File 'lib/admin_data/active_record_util.rb', line 4 def klass @klass end |
Instance Method Details
#declared_belongs_to_association_names ⇒ Object
class User
belongs_to :club
end
ActiveRecordUtil.new(User).declared_belongs_to_association_names #=> [‘club’]
26 27 28 |
# File 'lib/admin_data/active_record_util.rb', line 26 def declared_belongs_to_association_names delcared_association_names_for(:belongs_to).map(&:name).map(&:to_s) end |
#declared_habtm_association_names ⇒ Object
class User
has_and_belongs_to_many :clubs
end
ActiveRecordUtil.new(User).declared_habtm_association_names #=> [‘clubs’]
16 17 18 |
# File 'lib/admin_data/active_record_util.rb', line 16 def declared_habtm_association_names delcared_association_names_for(:has_and_belongs_to_many).map(&:name).map(&:to_s) end |
#declared_has_many_association_names ⇒ Object
class User
has_many :cars
end
ActiveRecordUtil.new(User).declared_has_many_association_names #=> [‘cars’]
46 47 48 |
# File 'lib/admin_data/active_record_util.rb', line 46 def declared_has_many_association_names delcared_association_names_for(:has_many).map(&:name).map(&:to_s) end |
#declared_has_one_association_names ⇒ Object
class User
has_one :car
end
ActiveRecordUtil.new(User).declared_has_one_association_names #=> [‘car’]
36 37 38 |
# File 'lib/admin_data/active_record_util.rb', line 36 def declared_has_one_association_names delcared_association_names_for(:has_one).map(&:name).map(&:to_s) end |
#habtm_klasses_for ⇒ Object
returns an array of classes
class User
has_and_belongs_to_many :clubs
end
ActiveRecordUtil.new(User).habtm_klasses_for #=> [Club]
58 59 60 61 62 |
# File 'lib/admin_data/active_record_util.rb', line 58 def habtm_klasses_for declared_habtm_association_names.map do |assoc_name| klass_for_association_type_and_name(:has_and_belongs_to_many, assoc_name) end end |
#has_association_info? ⇒ Boolean
returns false if the given Klass has no association info. Otherwise returns true.
81 82 83 84 85 86 |
# File 'lib/admin_data/active_record_util.rb', line 81 def has_association_info? declared_belongs_to_association_names.any? || declared_has_many_association_names.any? || declared_has_many_association_names.any? || declared_habtm_association_names.any? end |
#klass_for_association_type_and_name(association_type, association_name) ⇒ Object
returns a class or nil
class User
has_many :comments
end
ActiveRecordUtil.new(User).klass_for_association_type_and_name(:has_many, ‘comments’) #=> Comment
73 74 75 76 77 78 |
# File 'lib/admin_data/active_record_util.rb', line 73 def klass_for_association_type_and_name(association_type, association_name) data = klass.name.camelize.constantize.reflections.values.detect do |value| value.macro == association_type && value.name.to_s == association_name end data.klass if data # output of detect from hash is an array with key and value end |