Sha256: 70800f18555752355fb898e1d0f88a37ed28a3e81bde2e67c226e059af858320

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

module Meta2
  class Setting < ActiveRecord::Base
    with_options on: :pre_check do
      validates :section, presence: true, inclusion: { in: Meta2.config.sections }
    end

    with_options on: :formats do
      validates :app_name,  presence: true, uniqueness: {scope: [:section, :path, :name]}
      validates :name,      presence: true
      validates :value,     presence: true, format: {with: /\A.*\%.*\Z/i, message: I18n.t('meta2.setting.message.incorrect_format_for_formats_value')}
    end

    with_options on: :defaults do
      validates :app_name,  presence: true, uniqueness: {scope: [:section, :path, :name]}
      validates :name,      presence: true
      validates :value,     presence: true
    end

    with_options on: :parts do
      validates :app_name,  presence: true, uniqueness: {scope: [:section, :path, :name]}
      validates :path,      presence: true
      validates :name,      presence: true
      validates :value,     presence: true
    end


    scope :by_app_name, ->(app_name) {
      where app_name: app_name
    }

    scope :target_sections, ->(section_name) {
      section_name = 'parts' if section_name.blank?
      return unless self.sections.include? section_name
      send section_name
    }

    scope :formats, -> {
      where section: 'formats'
    }

    scope :defaults, -> {
      where section: 'defaults'
    }

    scope :parts, -> {
      where section: 'parts'
    }

    def method_missing(method, *args)
      check_name = method.to_s.gsub '?', ''
      unless %w(formats defaults parts).include? check_name
        super(method, args) and return
      end

      self.section == check_name
    end

    class << self
      def app_names
        self.group(:app_name).pluck :app_name
      end

      def sections
        Meta2.config.sections
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
meta2-0.0.3 app/models/meta2/setting.rb
meta2-0.0.2 app/models/meta2/setting.rb