Sha256: 77dba30f5d9492fcf673b5b2e2e3f237ce11ebde4029f2a122bfd8793392f66a

Contents?: true

Size: 1.4 KB

Versions: 16

Compression:

Stored size: 1.4 KB

Contents

module PaginationSearch
  class AttributeSet
    Attribute = Struct.new(:name, :arel_table, :association_name, :column_name, :type) do
      def column
        arel_table[column_name]
      end

      def matching_condition(value)
        column.matches("%#{value}%")
      end
    end

    def initialize(base_class, attribute_locations)
      @base_class = base_class
      @attributes = {}
      build_attributes(attribute_locations)
    end

    def [](name)
      @attributes[name]
    end

    def foreign_attributes
      @attributes.values.select do |attribute|
        foreign?(attribute.association_name)
      end
    end

    def text_attributes
      @attributes.values.select do |attribute|
        text?(attribute.type)
      end
    end

    private

    def foreign?(association)
      association != :base
    end

    def text?(type)
      type == :text
    end

    def build_attributes(attribute_locations)
      attribute_locations.each do |key, value|
        arel_table = arel_table_for_association(value[:association])
        @attributes[key] = Attribute.new(key, arel_table, value[:association], value[:column], value[:type])
      end
    end

    def arel_table_for_association(association)
      if foreign?(association)
        table_name = @base_class.reflections[association.to_s].plural_name
        Arel::Table.new(table_name)
      else
        @base_class.arel_table
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
aleph_analytics-0.4.9.pre.dev lib/pagination_search/attribute_set.rb
aleph_analytics-0.4.8 lib/pagination_search/attribute_set.rb
aleph_analytics-0.4.7 lib/pagination_search/attribute_set.rb
aleph_analytics-0.4.4 lib/pagination_search/attribute_set.rb
aleph_analytics-0.4.2 lib/pagination_search/attribute_set.rb
aleph_analytics-0.4.1 lib/pagination_search/attribute_set.rb
aleph_analytics-0.3.0 lib/pagination_search/attribute_set.rb
aleph_analytics-0.2.0 lib/pagination_search/attribute_set.rb
aleph_analytics-0.1.0 lib/pagination_search/attribute_set.rb
aleph_analytics-0.0.6 lib/pagination_search/attribute_set.rb
aleph_analytics-0.0.5 lib/pagination_search/attribute_set.rb
aleph_analytics-0.0.4 lib/pagination_search/attribute_set.rb
aleph_analytics-0.0.3 lib/pagination_search/attribute_set.rb
aleph_analytics-0.0.2 lib/pagination_search/attribute_set.rb
aleph_analytics-0.0.1.alpha lib/pagination_search/attribute_set.rb
aleph_analytics-0.0.0.alpha lib/pagination_search/attribute_set.rb