Sha256: dafa57ba9e5ee7f0927dd2c216f8dfef89771819d75d864c813ed23cf95e685c

Contents?: true

Size: 1.9 KB

Versions: 25

Compression:

Stored size: 1.9 KB

Contents

module Para
  module Component
    class Base < ActiveRecord::Base
      self.table_name = 'para_components'

      class_attribute :component_name

      def self.register(name, component)
        self.component_name = name
        Para::Component.registered_components[name] = component
      end

      def self.configurable_on(key, options = {})
        store_accessor(:configuration, key)
      end

      configurable_on :controller

      belongs_to :component_section, class_name: 'Para::ComponentSection'

      validates :identifier, :type, presence: true

      before_validation :ensure_slug

      scope :ordered, -> { order(position: :asc) }

      def name
        read_attribute(:name) || ::I18n.t(
          "components.component.#{ identifier }",
          default: identifier.humanize
        )
      end

      def exportable?
        false
      end

      def subclassable?
        false
      end

      def history?
        false
      end

      def self.model_name
        @model_name ||= ModelName.new(self)
      end

      def default_form_actions
        [:submit, :submit_and_edit, :submit_and_add_another, :cancel]
      end

      def to_param
        slug
      end

      # This method is used by the components configuration system to assign
      # updated attributes from the config file to the component.
      #
      # This is meant to be overriden by components that have to define specific
      # behavior, like for the Crud component
      #
      def update_with(attributes)
        assign_attributes(attributes)
      end

      private

      def ensure_slug
        self.slug = identifier.parameterize if slug.blank? || identifier_changed?
      end
    end

    class ModelName < ActiveModel::Name
      def route_key
        super.gsub(/(para_|component_|_component$)/, '')
      end

      def singular_route_key
        super.gsub(/(para_|component_|_component$)/, '')
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
para-0.8.10 app/models/para/component/base.rb
para-0.8.9 app/models/para/component/base.rb
para-0.8.8 app/models/para/component/base.rb
para-0.8.7 app/models/para/component/base.rb
para-0.8.5 app/models/para/component/base.rb
para-0.8.3.3 app/models/para/component/base.rb
para-0.8.3.2 app/models/para/component/base.rb
para-0.8.3.1 app/models/para/component/base.rb
para-0.8.3 app/models/para/component/base.rb
para-0.8.2.3 app/models/para/component/base.rb
para-0.8.2.2 app/models/para/component/base.rb
para-0.8.2.1 app/models/para/component/base.rb
para-0.8.2 app/models/para/component/base.rb
para-0.8.1 app/models/para/component/base.rb
para-0.8.0 app/models/para/component/base.rb
para-0.7.4.p1 app/models/para/component/base.rb
para-0.7.4 app/models/para/component/base.rb
para-0.7.3.p6 app/models/para/component/base.rb
para-0.7.3.p5 app/models/para/component/base.rb
para-0.7.3.p4 app/models/para/component/base.rb