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-0.0.36 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.0.34 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.1.09 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.1.07 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.1.06 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.1.05 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.1.04 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.1.03 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.0.33 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.1.02 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.1.01 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.0.29 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.0.28 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.0.27 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.0.24 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.0.23 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.0.22 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.0.21 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.0.20 app/presenters/redmine_extensions/base_presenter.rb
redmine_extensions-0.0.19 app/presenters/redmine_extensions/base_presenter.rb