Class: Pickel::Condition
- Inherits:
-
Object
- Object
- Pickel::Condition
- Defined in:
- lib/pickel/condition.rb
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#join_tables ⇒ Object
readonly
Returns the value of attribute join_tables.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#predicate ⇒ Object
readonly
Returns the value of attribute predicate.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Class Method Summary collapse
Instance Method Summary collapse
- #assign_column(key, target: klass, join_tables: []) ⇒ Object
- #build(value) ⇒ Object
-
#initialize(klass:, predicate:, column: nil, target: nil, join_tables: []) ⇒ Condition
constructor
A new instance of Condition.
Constructor Details
#initialize(klass:, predicate:, column: nil, target: nil, join_tables: []) ⇒ Condition
Returns a new instance of Condition.
16 17 18 19 20 21 22 |
# File 'lib/pickel/condition.rb', line 16 def initialize(klass:, predicate:, column: nil, target: nil, join_tables: []) @klass = klass @predicate = predicate @column = column @target = target @join_tables = join_tables end |
Instance Attribute Details
#column ⇒ Object (readonly)
Returns the value of attribute column.
23 24 25 |
# File 'lib/pickel/condition.rb', line 23 def column @column end |
#join_tables ⇒ Object (readonly)
Returns the value of attribute join_tables.
23 24 25 |
# File 'lib/pickel/condition.rb', line 23 def join_tables @join_tables end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
23 24 25 |
# File 'lib/pickel/condition.rb', line 23 def klass @klass end |
#predicate ⇒ Object (readonly)
Returns the value of attribute predicate.
23 24 25 |
# File 'lib/pickel/condition.rb', line 23 def predicate @predicate end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
23 24 25 |
# File 'lib/pickel/condition.rb', line 23 def target @target end |
Class Method Details
Instance Method Details
#assign_column(key, target: klass, join_tables: []) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/pickel/condition.rb', line 25 def assign_column(key, target: klass, join_tables: []) base = { klass: klass, predicate: predicate, target: target, join_tables: join_tables } return Condition.new(base.merge(column: key)) if target.attribute_names.include?(key) association = target.reflect_on_all_associations.find { |a| key.start_with?(a.name.to_s) } if association new_key = key.delete_prefix("#{association.name}_") join_tables << association.name new_target = association.klass assign_column(new_key, target: new_target, join_tables: join_tables) else Condition.new(base.merge(predicate: nil)) end end |
#build(value) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/pickel/condition.rb', line 41 def build(value) return klass.all if predicate.nil? rel = predicate.build(target, column, value) join_tables.empty? ? rel : join_relation.merge(rel) end |