Sha256: 784f8bc25652c4d6c9836193d4f4c9e95539fa0eba50704953c2f5883ff81392

Contents?: true

Size: 2 KB

Versions: 3

Compression:

Stored size: 2 KB

Contents

module BitCore
  # Modeled after the presenter pattern. Ties data layer to view layer.
  class ContentProvider < ActiveRecord::Base
    belongs_to :content_module,
               class_name: "BitCore::ContentModule",
               foreign_key: :bit_core_content_module_id,
               inverse_of: :content_providers
    belongs_to :source_content, polymorphic: true

    validates :content_module,
              :type,
              :bit_core_content_module_id,
              :position,
              presence: true
    validates :position,
              numericality: { greater_than_or_equal_to: 1 },
              uniqueness: { scope: :bit_core_content_module_id }
    validates :show_next_nav, inclusion: { in: [true, false] }
    validate :template_path_exists
    validate :data_class_exists
    validate :data_attributes_exist

    serialize :data_attributes
    serialize :locals

    delegate :context, to: :content_module, prefix: false

    def exists?(_position)
      false
    end

    def data_class
      data_class_name.constantize
    rescue NameError
      nil
    end

    # compatibility method
    def show_nav_link?
      show_next_nav
    end

    private

    def template_path_exists
      path = File.join(Rails.root, "app", "views", template_path || "")
      return if Dir.exist?(path)

      errors.add(:template_path, "not found at #{ path }")
    end

    def data_class_exists
      return unless data_class_name && !data_class

      errors.add(:data_class_name,
                 "unable to find class '#{ data_class_name }'")
    end

    def data_attributes_exist
      return unless data_attributes
      attr_names = data_class.try(:attribute_names) || []
      unknown_attrs = data_attributes.select do |a|
        (a.class == Symbol || a.class == String) && !attr_names.include?(a.to_s)
      end
      return if unknown_attrs.count == 0

      errors.add(:data_attributes, "must be attributes on the model class " \
                 "(unrecognized: #{ unknown_attrs.join(", ") })")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bit_core-1.2.0 app/models/bit_core/content_provider.rb
bit_core-1.1.6 app/models/bit_core/content_provider.rb
bit_core-1.1.5 app/models/bit_core/content_provider.rb