Sha256: 83bc23c311b03983cbb0ca29cafb68579046206b13c7ba5aea0ddaf1d951311f

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

module Ecm::Tags
  class TagSearch
    include ActiveModel::Model

    attr_accessor :tag_list, :result, :taggable_classes, :fuzzy

    def self.call(*args)
      new(*args).do_work
    end

    def do_work
      say "#{self.class.name} running..."
      output = result
      output.each do |klass, resources|
        say "Found #{resources.size} #{klass.constantize.model_name.human(count: :other)}:", indent: 1
        say resources.inspect, indent: 2
      end
      say 'done'
      output
    end

    def tag_list
      @tag_list ||= ''
    end

    def result
      @result ||= search
    end

    def result!
      @result = nil
      result
    end

    def taggable_classes
      @taggable_classes ||= Ecm::Tags::Tagging.uniq.pluck(:taggable_type)
    end

    def fuzzy
      @fuzzy ||= false
    end

    def fuzzy=(value)
      @fuzzy = ActiveRecord::Type::Boolean.new.type_cast_from_database(value)
    end

    private

    def say(what, options = {})
      options.reverse_merge!(indent: 0)
      indent = options.delete(:indent)

      puts "#{' ' * indent * 2}#{what}"
    end

    def wild
      @wild ||= fuzzy
    end

    def any
      @any ||= fuzzy
    end

    def search
      taggable_classes.each_with_object({}) { |klass, result| result[klass] = klass.constantize.tagged_with(tag_list.split(', '), any: any, wild: wild) }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ecm_tags-0.0.4 app/models/ecm/tags/tag_search.rb
ecm_tags-0.0.3 app/models/ecm/tags/tag_search.rb
ecm_tags-0.0.2 app/models/ecm/tags/tag_search.rb
ecm_tags-0.0.1 app/models/ecm/tags/tag_search.rb