Sha256: 206185e8c25ab00cb039d9a59e55f2aa4a666bc57fb7223b28859be7bb6a4065

Contents?: true

Size: 1.37 KB

Versions: 6

Compression:

Stored size: 1.37 KB

Contents

module Effective
  class Region < ActiveRecord::Base
    self.table_name = EffectiveRegions.regions_table_name.to_s

    belongs_to :regionable, polymorphic: true, optional: true

    effective_resource do
      title     :string
      content   :text
      snippets  :text

      timestamps
    end

    serialize :snippets, HashWithIndifferentAccess

    scope :global, -> { where("#{EffectiveRegions.regions_table_name}.regionable_type IS NULL").where("#{EffectiveRegions.regions_table_name}.regionable_id IS NULL") }
    scope :with_snippets, -> { where("#{EffectiveRegions.regions_table_name}.snippets ILIKE ?", '%snippet_%') }

    validates :title, presence: true

    def snippets
      self[:snippets] || HashWithIndifferentAccess.new()
    end

    # Hash of the Snippets objectified
    #
    # Returns a Hash of {'snippet_1' => CurrentUserInfo.new(snippets[:key]['options'])}
    def snippet_objects(locals = {})
      locals = {} unless locals.kind_of?(Hash)
      @snippet_objects ||= snippets.map do |key, snippet|  # Key here is 'snippet_1'
        if snippet['class_name']
          klass = "Effective::Snippets::#{snippet['class_name'].classify}".safe_constantize
          klass.new(snippet.merge!(locals).merge!(:region => self, :id => key)) if klass
        end
      end.compact
    end

    def global?
      regionable_id == nil && regionable_type == nil
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
effective_regions-1.11.0 app/models/effective/region.rb
effective_regions-1.10.3 app/models/effective/region.rb
effective_regions-1.10.2 app/models/effective/region.rb
effective_regions-1.10.1 app/models/effective/region.rb
effective_regions-1.10.0 app/models/effective/region.rb
effective_regions-1.9.0 app/models/effective/region.rb