Sha256: cea2ae8864852ef80607cdd3439564ad700bc82679778a12259f110b62da5f68

Contents?: true

Size: 1.27 KB

Versions: 88

Compression:

Stored size: 1.27 KB

Contents

module RedmineExtensions
  class BasePresenter < SimpleDelegator
    attr_reader :model, :options

    def self.registered_presenters
      @registered_presenters ||= {}
    end

    def self.register(klass_name, *for_classes)
      for_classes = [klass_name.sub(/Presenter$/, '')] unless for_classes.any?
      for_classes.each do |name|
        registered_presenters[name] = klass_name
      end
    end

    def self.presenter_for(model)
      klasses = model.hiearchy.map do |klass|
        (registered_presenters[klass] || "#{klass}Presenter").constantize rescue nil
      end.compact
      raise NameError, 'presenter for ' + model.class.name + ' is not registered' unless klasses.any?
      klasses.first
    end

    def self.present(model, view, options={})
      presenter_for(model).new(model, view, options)
    end

    def initialize(model, view, options={})
      @model, @view, @options = model, view, options
      super(@model)
    end

    def update_options(options={})
      @view = options.delete(:view_context) if options.key?(:view_context)
      @options.merge!(options)
      self
    end

    def to_model
      @model || self
    end

    def h
      @view
    end

    protected
      def model=(model)
        @model = model
        __setobj__(model)
      end

  end
end

Version data entries

88 entries across 88 versions & 1 rubygems

Version Path
redmine_extensions-1.2.0 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-1.1.2 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-1.1.1 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-1.1.0 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-1.0.0 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.6.3 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.6.2 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.6.1 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.6.0 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.4.5 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.5.1 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.4.4 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.4.3 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.4.1 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.4.0 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.4.0.beta app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.3.11 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.3.10 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.3.9 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.3.8 app/presenters/redmine_extensions/base_presenter.rb