Sha256: ddd280add24c688eaaa3711860ddc5893df65e5f293bf5d495205478a75f3fb3

Contents?: true

Size: 1.82 KB

Versions: 30

Compression:

Stored size: 1.82 KB

Contents

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

module SubjModels

  module BrandModule

    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
        has_many :events

        has_and_belongs_to_many :users
        has_and_belongs_to_many :action_banners

        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 :in_index_list, -> condition { where(show_on_index: condition) }
        scope :in_recommended_list, -> condition { where(is_recommended: condition) }

        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

        scope :has_events, -> do
          joins(events: :event_schedule).where('event_schedules.event_date >= ?',  DateTime.now.beginning_of_day).uniq
        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

30 entries across 30 versions & 2 rubygems

Version Path
subj_sub_models-0.0.3.16 lib/subj_models/brand.rb
subj_sub_models-0.0.3.15 lib/subj_models/brand.rb
subj_sub_models-0.0.3.14 lib/subj_models/brand.rb
subj_sub_models-0.0.3.13 lib/subj_models/brand.rb
subj_sub_models-0.0.3.12 lib/subj_models/brand.rb
subj_sub_models-0.0.3.11 lib/subj_models/brand.rb
subj_sub_models-0.0.3.10 lib/subj_models/brand.rb
subj_sub_models-0.0.3.9 lib/subj_models/brand.rb
subj_sub_models-0.0.3.8 lib/subj_models/brand.rb
subj_sub_models-0.0.3.7 lib/subj_models/brand.rb
subj_sub_models-0.0.3.6 lib/subj_models/brand.rb
subj_sub_models-0.0.3.5 lib/subj_models/brand.rb
subj_sub_models-0.0.3.4 lib/subj_models/brand.rb
subj_sub_models-0.0.3.3 lib/subj_models/brand.rb
subj_sub_models-0.0.3.1 lib/subj_models/brand.rb
subj_sub_models-0.0.3 lib/subj_models/brand.rb
subj_sub_models-0.0.2 lib/subj_models/brand.rb
subj_sub_models-0.0.1 lib/subj_models/brand.rb
subj_models-0.6.9 lib/subj_models/brand.rb
subj_models-0.6.8 lib/subj_models/brand.rb