Sha256: 8520b68f45af04d0b74c4853cd66dcbd6bd363febf7dc27fc76022c6fff50735

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

# == Schema Information
#
# Table name: formats_services
#
#  id                  :integer          not null, primary key
#  title_translations  :hstore           default({})
#  color_theme_id      :integer
#  default_category_id :integer
#  created_at          :datetime
#  updated_at          :datetime
#
module Formats
  class Service < ActiveRecord::Base
    has_many :categories, class_name: 'Formats::Category', dependent: :destroy
    include TranslationCms::WhiteList

    translates :title, :description
    white_list :description
    before_save :save_default_category

    def color_theme
      Formats::ColorTheme.find(color_theme_id)
    end

    scope :sorted, -> { order("#{quoted_table_name}.sort_order") }

    def default_category
      @default_category ||= Category.find_by(id: default_category_id) ||
                            categories.build(title: title.try(:upcase))
    end

    class << self
      def sunrise_search(params)
        q = normal
        q = q.where('title LIKE ?', "%#{params[:title]}%") if params[:title].present?
        q
      end
    end

    protected

    def save_default_category
      default_category.save
      self[:default_category_id] = default_category.id
    end

    def categories_with_filter_default(*args, &block)
      categories_without_filter_default(*args, &block).where.not(id: default_category_id)
    end
    alias_method_chain :categories, :filter_default
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
translation_cms-0.1.5 app/models/formats/service.rb