Class: ArOuterJoins::Join
- Inherits:
-
Object
- Object
- ArOuterJoins::Join
- Defined in:
- lib/ar_outer_joins/join.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
- #apply(*args) ⇒ Object
- #generate(*args) ⇒ Object
-
#initialize(klass) ⇒ Join
constructor
A new instance of Join.
Constructor Details
#initialize(klass) ⇒ Join
Returns a new instance of Join.
5 6 7 |
# File 'lib/ar_outer_joins/join.rb', line 5 def initialize(klass) @klass = klass end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
3 4 5 |
# File 'lib/ar_outer_joins/join.rb', line 3 def klass @klass end |
Instance Method Details
#apply(*args) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ar_outer_joins/join.rb', line 22 def apply(*args) scope = if klass.all.is_a?(ActiveRecord::Relation) then klass.all else klass.scoped end joins = scope.joins_values.select.map { |j| j.to_sql if j.respond_to?(:to_sql) } generate(*args).flatten.inject(scope) do |scope, join| if joins.include?(join.to_sql) scope else scope.joins(join) end end end |
#generate(*args) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ar_outer_joins/join.rb', line 9 def generate(*args) args.flatten.compact.map do |arg| if arg.is_a?(Hash) arg.map do |key, value| association = klass.reflect_on_association(key) generate(key) + Join.new(association.klass).generate(value) end else JoinBuilder.new(klass.reflect_on_association(arg)).build end end end |