Class: SimpleQuery::JoinClause
- Inherits:
-
Object
- Object
- SimpleQuery::JoinClause
- Defined in:
- lib/simple_query/clauses/join_clause.rb
Instance Attribute Summary collapse
-
#joins ⇒ Object
readonly
Returns the value of attribute joins.
Instance Method Summary collapse
- #add(table1, table2, foreign_key:, primary_key:, join_type: :inner) ⇒ Object
- #apply_to(query) ⇒ Object
-
#initialize ⇒ JoinClause
constructor
A new instance of JoinClause.
Constructor Details
#initialize ⇒ JoinClause
Returns a new instance of JoinClause.
7 8 9 |
# File 'lib/simple_query/clauses/join_clause.rb', line 7 def initialize @joins = [] end |
Instance Attribute Details
#joins ⇒ Object (readonly)
Returns the value of attribute joins.
5 6 7 |
# File 'lib/simple_query/clauses/join_clause.rb', line 5 def joins @joins end |
Instance Method Details
#add(table1, table2, foreign_key:, primary_key:, join_type: :inner) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/simple_query/clauses/join_clause.rb', line 11 def add(table1, table2, foreign_key:, primary_key:, join_type: :inner) @joins << { table1: to_arel_table(table1), table2: to_arel_table(table2), foreign_key: foreign_key, primary_key: primary_key, type: join_type } end |
#apply_to(query) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/simple_query/clauses/join_clause.rb', line 21 def apply_to(query) @joins.each do |join_def| table1 = join_def[:table1] table2 = join_def[:table2] fk = join_def[:foreign_key] pk = join_def[:primary_key] type = join_def[:type] join_class = case type when :left Arel::Nodes::OuterJoin when :right Arel::Nodes::RightOuterJoin when :full Arel::Nodes::FullOuterJoin else Arel::Nodes::InnerJoin end condition = table2[fk].eq(table1[pk]) query.join(table2, join_class).on(condition) end query end |