Sha256: 16cc2e131720c10b81240b02d56b76ebc592ad78ab9edff86a128e695841f90e

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'ar_finder_form'

module ArFinderForm
  class JoinedTable < Table
    attr_reader :reflection, :join_type, :parent_table
    def initialize(parent_table, join_type, reflection, *args)
      super(reflection.klass, *args)
      @parent_table = parent_table
      @join_type = join_type
      @reflection = reflection
      @table_name = reflection.klass.table_name
      @name = "cond_" << @table_name
    end

    def table_name; @table_name; end
    def name; @name; end

    def root_table
      parent_table.root_table
    end

    def build(context)
      sub_context = context.new_sub_context(:connector => 'AND')
      super(sub_context)
      unless sub_context.empty?
        context.merge(sub_context) do
          context.joins << build_join
        end
      end
    end

    def build_join
      join_on =
        case reflection.macro
        when :belongs_to then
          "#{name}.id = #{parent_table.name}.#{reflection.primary_key_name}"
        else
          "#{name}.#{reflection.primary_key_name} = #{parent_table.name}.id"
        end
      "%s JOIN %s %s ON %s" % [
        join_type.to_s.gsub(/\_/, ' ').upcase, reflection.klass.table_name, name, join_on
      ]
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ar_finder_form-0.1.0 lib/ar_finder_form/joined_table.rb