lundi 18 juillet 2016

Improving this mix in

I’m trying to improve this mixin and the one thing that’s bothering me is I can’t seem got get the rest of the module to know about the base property in the self.included(base) so I’m having to pass base to every module method. Is there a better way of doing this:

module SearchSort
      def self.included(base)
        # binds included class's class methods
        base.send :extend, ClassMethods
        initialize_scopes(base)
      end

      def self.initialize_scopes(base)
        initialize_type_scope(base)
        initialize_product_name_scope(base)
      end

      def self.initialize_type_scope(base)
        base.scope :for_work_type, lambda { |work_type|
        Rails.logger.debug("-----(45) work_type #{work_type}")
        terms = process_terms(work_type)
      base.where(
        terms.map { '(LOWER(workable_type) LIKE ?)' }.join(' AND '),
        *terms.map { |e| [e] * 1 }.flatten)
    }
  end

  def self.initialize_product_name_scope(base)
    base.scope :for_product_name, lambda { |product_name|
      terms = process_terms(product_name)
      base.where(
        terms.map { '(LOWER(products.name) LIKE ?)' }.join(' AND '),
        *terms.map { |e| [e] * 1 }.flatten
      ).joins(:product)
    }
  end

  module ClassMethods
    def pid_opts
      [%w(Newly\ Added newly_added), %w(Waiting waiting),
       %w(Ready ready), %w(Working working),
       %w(Error error), %w('Error Retry', 'error_retry'),
       %w(Done done), %w(Gated gated)
      ]
    end
  end
end

Aucun commentaire:

Enregistrer un commentaire