Sha256: 1cfe08c17623ac90f389ec6eb3ffe638be99b6cc57f334d2dfc36eb95885d0d5

Contents?: true

Size: 1.18 KB

Versions: 6

Compression:

Stored size: 1.18 KB

Contents

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

    belongs_to :regionable, :polymorphic => true

    structure do
      title             :string, :validates => [:presence]
      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") }

    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
      @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!(: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.2.2 app/models/effective/region.rb
effective_regions-1.2.1 app/models/effective/region.rb
effective_regions-1.2.0 app/models/effective/region.rb
effective_regions-1.1.1 app/models/effective/region.rb
effective_regions-1.1.0 app/models/effective/region.rb
effective_regions-1.0.0 app/models/effective/region.rb