Class: PartialKs::FilteredTable
- Inherits:
-
Object
- Object
- PartialKs::FilteredTable
- Defined in:
- lib/partial_ks/filtered_table.rb
Instance Attribute Summary collapse
-
#custom_filter_relation ⇒ Object
readonly
Returns the value of attribute custom_filter_relation.
-
#parent_model ⇒ Object
readonly
Returns the value of attribute parent_model.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
-
#initialize(table, parent_model, custom_filter_relation: nil) ⇒ FilteredTable
constructor
A new instance of FilteredTable.
- #kitchen_sync_filter ⇒ Object
Constructor Details
#initialize(table, parent_model, custom_filter_relation: nil) ⇒ FilteredTable
Returns a new instance of FilteredTable.
5 6 7 8 9 |
# File 'lib/partial_ks/filtered_table.rb', line 5 def initialize(table, parent_model, custom_filter_relation: nil) @table = table @parent_model = parent_model @custom_filter_relation = custom_filter_relation end |
Instance Attribute Details
#custom_filter_relation ⇒ Object (readonly)
Returns the value of attribute custom_filter_relation.
2 3 4 |
# File 'lib/partial_ks/filtered_table.rb', line 2 def custom_filter_relation @custom_filter_relation end |
#parent_model ⇒ Object (readonly)
Returns the value of attribute parent_model.
2 3 4 |
# File 'lib/partial_ks/filtered_table.rb', line 2 def parent_model @parent_model end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
2 3 4 |
# File 'lib/partial_ks/filtered_table.rb', line 2 def table @table end |
Instance Method Details
#kitchen_sync_filter ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/partial_ks/filtered_table.rb', line 11 def kitchen_sync_filter filter_condition = custom_filter_relation || parent_model if !filter_condition.nil? if filter_condition.is_a?(ActiveRecord::Relation) || filter_condition.respond_to?(:where_sql) only_filter = filter_condition.where_sql.to_s.sub("WHERE", "") elsif filter_condition.is_a?(String) only_filter = filter_condition else # this only supports parents where it's a belongs_to # TODO we can make it work with has_many # e.g. SomeModel.reflect_on_association(:elses) association = table.model.reflect_on_all_associations(:belongs_to).find {|assoc| assoc.class_name == filter_condition.name} raise "#{filter_condition.name} not found in #{table.model.name} associations" if association.nil? only_filter = "#{association.foreign_key} IN (#{[0, *filter_condition.pluck(:id)].join(',')})" end {"only" => only_filter} end end |