Sha256: 9cc53b12a625d10449e668335b3284dbea69917a24250648004115642b405606

Contents?: true

Size: 1.86 KB

Versions: 9

Compression:

Stored size: 1.86 KB

Contents

module MakeTaggable::Taggable::TaggedWithQuery
  class QueryBase
    def initialize(taggable_model, tag_model, tagging_model, tag_list, options)
      @taggable_model = taggable_model
      @tag_model = tag_model
      @tagging_model = tagging_model
      @tag_list = tag_list
      @options = options
    end

    private

    attr_reader :taggable_model, :tag_model, :tagging_model, :tag_list, :options

    def taggable_arel_table
      @taggable_arel_table ||= taggable_model.arel_table
    end

    def tag_arel_table
      @tag_arel_table ||= tag_model.arel_table
    end

    def tagging_arel_table
      @tagging_arel_table ||= tagging_model.arel_table
    end

    def tag_match_type(tag)
      matches_attribute = tag_arel_table[:name]
      matches_attribute = matches_attribute.lower unless MakeTaggable.strict_case_match

      if options[:wild].present?
        matches_attribute.matches("%#{escaped_tag(tag)}%", "!", MakeTaggable.strict_case_match)
      else
        matches_attribute.matches(escaped_tag(tag), "!", MakeTaggable.strict_case_match)
      end
    end

    def tags_match_type
      matches_attribute = tag_arel_table[:name]
      matches_attribute = matches_attribute.lower unless MakeTaggable.strict_case_match

      if options[:wild].present?
        matches_attribute.matches_any(tag_list.map { |tag| "%#{escaped_tag(tag)}%" }, "!", MakeTaggable.strict_case_match)
      else
        matches_attribute.matches_any(tag_list.map { |tag| escaped_tag(tag).to_s }, "!", MakeTaggable.strict_case_match)
      end
    end

    def escaped_tag(tag)
      tag = tag.downcase unless MakeTaggable.strict_case_match
      MakeTaggable::Utils.escape_like(tag)
    end

    def adjust_taggings_alias(taggings_alias)
      if taggings_alias.size > 75
        taggings_alias = "taggings_alias_" + Digest::SHA1.hexdigest(taggings_alias)
      end
      taggings_alias
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
make_taggable-0.7.5 lib/make_taggable/taggable/tagged_with_query/query_base.rb
make_taggable-0.7.4 lib/make_taggable/taggable/tagged_with_query/query_base.rb
make_taggable-0.7.3 lib/make_taggable/taggable/tagged_with_query/query_base.rb
make_taggable-0.7.2 lib/make_taggable/taggable/tagged_with_query/query_base.rb
make_taggable-0.7.1 lib/make_taggable/taggable/tagged_with_query/query_base.rb
make_taggable-0.7.0 lib/make_taggable/taggable/tagged_with_query/query_base.rb
make_taggable-0.6.6 lib/make_taggable/taggable/tagged_with_query/query_base.rb
make_taggable-0.6.5 lib/make_taggable/taggable/tagged_with_query/query_base.rb
make_taggable-0.6.4 lib/make_taggable/taggable/tagged_with_query/query_base.rb