Class: ParamsReady::Query::Join
- Inherits:
-
Object
- Object
- ParamsReady::Query::Join
- Defined in:
- lib/params_ready/query/join_clause.rb
Defined Under Namespace
Classes: Builder
Instance Attribute Summary collapse
-
#arel_table(context, parameter) ⇒ Object
readonly
Returns the value of attribute arel_table.
-
#statement ⇒ Object
readonly
Returns the value of attribute statement.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #arel_type(type) ⇒ Object
- #eligible?(context, parameter) ⇒ Boolean
-
#initialize(table, type, table_alias: nil, &block) ⇒ Join
constructor
A new instance of Join.
- #to_arel(joined_table, base_table, context, parameter) ⇒ Object
Constructor Details
#initialize(table, type, table_alias: nil, &block) ⇒ Join
Returns a new instance of Join.
34 35 36 37 38 39 |
# File 'lib/params_ready/query/join_clause.rb', line 34 def initialize(table, type, table_alias: nil, &block) @arel_table = Helpers::ArelBuilder::Table.instance(table, table_alias: table_alias) @type = arel_type(type) @statement, @only_if = Builder.new(&block).build freeze end |
Instance Attribute Details
#arel_table(context, parameter) ⇒ Object (readonly)
Returns the value of attribute arel_table.
32 33 34 |
# File 'lib/params_ready/query/join_clause.rb', line 32 def arel_table @arel_table end |
#statement ⇒ Object (readonly)
Returns the value of attribute statement.
32 33 34 |
# File 'lib/params_ready/query/join_clause.rb', line 32 def statement @statement end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
32 33 34 |
# File 'lib/params_ready/query/join_clause.rb', line 32 def type @type end |
Instance Method Details
#arel_type(type) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/params_ready/query/join_clause.rb', line 45 def arel_type(type) case type when :inner then Arel::Nodes::InnerJoin when :outer then Arel::Nodes::OuterJoin else raise ParamsReadyError, "Unimplemented join type '#{type}'" end end |
#eligible?(context, parameter) ⇒ Boolean
61 62 63 64 65 |
# File 'lib/params_ready/query/join_clause.rb', line 61 def eligible?(context, parameter) return true if @only_if.nil? @only_if.call(context, parameter) end |
#to_arel(joined_table, base_table, context, parameter) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/params_ready/query/join_clause.rb', line 53 def to_arel(joined_table, base_table, context, parameter) return joined_table unless eligible?(context, parameter) arel_table = arel_table(context, parameter) join_statement = @statement.to_arel(base_table, arel_table, context, parameter) joined_table.join(arel_table, @type).on(join_statement) end |