Sha256: dfea8362cdc01a1d56961a1b127fe4e9a051baacbaffc9d3ec3ceaca52bc0e0f

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require 'subj_models/concerns/comprising_external_id'
require 'subj_models/concerns/shared_scopes'

module SubjModels

  module Brand

    def self.included(including_class)

      including_class.class_eval do

        include SubjModels::SharedScopes
        include SubjModels::ComprisingExternalId

        has_many :brand_lines, dependent: :destroy
        #has_many :nomenclatures
        #has_many :videos

        #belongs_to :content_block1, class_name: "ContentBlock"
        #belongs_to :content_block2, class_name: "ContentBlock"
        #belongs_to :content_block3, class_name: "ContentBlock"
        belongs_to :document_file

        before_validation :set_first_letter, if: :name_changed?

        validates :name, presence: true

        scope :category_id, -> (category_id) do
          return all if category_id.blank?
          joins(brand_lines: [{ nomenclatures: :category}]).where('categories.id' => category_id)
        end

        scope :by_first_letters, -> (by_first_letters) do
          unless by_first_letters.blank?
            where(first_letter: by_first_letters)
          end
        end

      end

    end

    def to_s
      name
    end

    private

    def self.searchable_fields
      [:name, :hidden_name]
    end

    def order_params
      [ :order_by_name, :order_by_popularity ]
    end

    def set_first_letter
      self.first_letter = name[0].downcase
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
subj_models-0.3.8 lib/subj_models/brand.rb
subj_models-0.3.7 lib/subj_models/brand.rb