Class: PartialKs::Table
- Inherits:
-
Object
- Object
- PartialKs::Table
- Defined in:
- lib/partial_ks/table.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
-
#candidate_parent_classes ⇒ Object
NB: can’t do polymorphic for now, rails errors on reflection#klass see, e.g.
-
#initialize(model) ⇒ Table
constructor
A new instance of Table.
-
#model? ⇒ Boolean
sometimes the table is present, but the model is not defined e.g.
- #parent_tables ⇒ Object
- #relation_for_associated_model(klass) ⇒ Object
- #top_level_table? ⇒ Boolean
Constructor Details
#initialize(model) ⇒ Table
6 7 8 |
# File 'lib/partial_ks/table.rb', line 6 def initialize(model) @model = model end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
3 4 5 |
# File 'lib/partial_ks/table.rb', line 3 def model @model end |
Instance Method Details
#candidate_parent_classes ⇒ Object
NB: can’t do polymorphic for now, rails errors on reflection#klass see, e.g. github.com/rails/rails/issues/15833
22 23 24 |
# File 'lib/partial_ks/table.rb', line 22 def candidate_parent_classes non_nullable_reflections.reject(&:polymorphic?).map(&:klass) end |
#model? ⇒ Boolean
sometimes the table is present, but the model is not defined e.g. in market specific tables
12 13 14 |
# File 'lib/partial_ks/table.rb', line 12 def model? model && model.respond_to?(:table_name) end |
#parent_tables ⇒ Object
26 27 28 |
# File 'lib/partial_ks/table.rb', line 26 def parent_tables belongs_to_reflections.map(&:table_name) end |
#relation_for_associated_model(klass) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/partial_ks/table.rb', line 30 def relation_for_associated_model(klass) association = model.reflect_on_all_associations.find {|assoc| assoc.class_name == klass.name} raise "#{filter_condition.name} not found in #{model.name} associations" if association.nil? case association.macro when :belongs_to model.where(association.foreign_key => [0, *klass.pluck(:id)]) when :has_many model.where(model.primary_key => [0, *klass.pluck(association.foreign_key)]) else raise "Unknown macro" end end |
#top_level_table? ⇒ Boolean
16 17 18 |
# File 'lib/partial_ks/table.rb', line 16 def top_level_table? candidate_parent_classes.empty? end |