Sha256: b1787251ab2f5ec79dbb4e234316f2f14bf96c489b22bf629815e5abecf6b665

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

module Hydra
  module ActiveModelPresenter
    extend ActiveSupport::Concern
    included do
      attr_reader :model
      class_attribute :model_class
    end

    def initialize(object)
      @model = object
    end

    def to_key
      model.to_key
    end

    def to_param
      model.to_param
    end

    def to_model
      model.to_model
    end

    def persisted?
      model.persisted?
    end

    def [](key)
      model[key]
    end

    module ClassMethods
      def model_name
        if model_class.nil?
          raise "You must set `self.model_class = ' after including Hydra::Presenter on #{self}."
        end
        model_class.model_name
      end
    end
  end

  module Presenter
    extend ActiveSupport::Concern
    include ActiveModelPresenter
    included do
      class_attribute :_terms, instance_accessor: false
    end

    def terms
      self.class._terms
    end

    module ClassMethods
      def multiple?(field)
        model_class.multiple?(field)
      end

      def unique?(field)
        model_class.unique?(field)
      end

      def terms=(terms)
        self._terms = terms
        create_term_accessors(terms)
      end

      def terms
        self._terms
      end

      private

        def create_term_accessors(terms)
          # we delegate to the array accessor, because the array accessor is overridden in MultiForm
          # which is included by GenericFileEditForm
          terms.each do |term|
            next if method_defined? term
            define_method term do
              self[term]
            end
          end
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hydra-editor-1.0.2 app/presenters/hydra/presenter.rb
hydra-editor-1.0.1 app/presenters/hydra/presenter.rb
hydra-editor-1.0.0 app/presenters/hydra/presenter.rb