Class: RailsBestPractices::Core::ModelAssociations
- Inherits:
-
Object
- Object
- RailsBestPractices::Core::ModelAssociations
- Defined in:
- lib/rails_best_practices/core/model_associations.rb
Overview
Model associations container.
Instance Method Summary collapse
- #add_association(model_name, association_name, association_meta, association_class = nil) ⇒ Object
-
#each(&block) ⇒ Object
delegate each to @associations.
-
#get_association(model_name, association_name) ⇒ Hash
Get a model association.
-
#get_association_class_name(table_name, association_name) ⇒ String
Get association’s class name.
-
#initialize ⇒ ModelAssociations
constructor
A new instance of ModelAssociations.
-
#is_association?(model_name, association_name) ⇒ Boolean
If it is a model’s association.
Constructor Details
#initialize ⇒ ModelAssociations
Returns a new instance of ModelAssociations.
6 7 8 |
# File 'lib/rails_best_practices/core/model_associations.rb', line 6 def initialize @associations = {} end |
Instance Method Details
#add_association(model_name, association_name, association_meta, association_class = nil) ⇒ Object
15 16 17 18 |
# File 'lib/rails_best_practices/core/model_associations.rb', line 15 def add_association(model_name, association_name, , association_class=nil) @associations[model_name] ||= {} @associations[model_name][association_name] = {"meta" => , "class_name" => association_class || association_name.classify} end |
#each(&block) ⇒ Object
delegate each to @associations.
41 42 43 |
# File 'lib/rails_best_practices/core/model_associations.rb', line 41 def each(&block) @associations.each { |model, model_associations| yield model, model_associations } end |
#get_association(model_name, association_name) ⇒ Hash
Get a model association.
25 26 27 28 |
# File 'lib/rails_best_practices/core/model_associations.rb', line 25 def get_association(model_name, association_name) associations = @associations[model_name] associations and associations[association_name] end |
#get_association_class_name(table_name, association_name) ⇒ String
Get association’s class name
50 51 52 53 54 |
# File 'lib/rails_best_practices/core/model_associations.rb', line 50 def get_association_class_name(table_name, association_name) associations = @associations.select { |model, model_associations| model.gsub("::", "").tableize == table_name }.values.first and = associations.select { |name, | name == association_name }.values.first and ["class_name"] end |
#is_association?(model_name, association_name) ⇒ Boolean
If it is a model’s association.
35 36 37 38 |
# File 'lib/rails_best_practices/core/model_associations.rb', line 35 def is_association?(model_name, association_name) associations = @associations[model_name] associations && associations[association_name] end |