Sha256: ee389863051e897e09726c7d1d056e0f7403ec2dfba4c3a025145a97300f7539

Contents?: true

Size: 968 Bytes

Versions: 3

Compression:

Stored size: 968 Bytes

Contents

module PeekAView
  class View < ActiveSupport::OrderedOptions
    def initialize(name, common_block = nil)
      @name       = name
      self.params = nil # just to initialize params
      @blocks = []
      record(common_block) if common_block
    end

    def record(block)
      @blocks << block
    end

    def template
      super || @name
    end

    def layout
      'application' # TODO be smarter
    end

    def params=(new_params)
      *controller, action = template.split('/')
      augmented = (new_params || { }).reverse_merge(
        controller: controller.join('/'),
        action:     action
      )
      super(augmented)
      augmented
    end

    def fixture(name)
      fixture_dir = Rails.root + 'spec/fixtures/peek_a_view' # TODO config
      file = File.join(fixture_dir, name)
      File.read(file)
    end

    def variables(options = {})
      @blocks.flatten.each { |block| block.call(self, options) }
      self
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
peek-a-view-0.0.4 lib/peek_a_view/view.rb
peek-a-view-0.0.3 lib/peek_a_view/view.rb
peek-a-view-0.0.2 lib/peek_a_view/view.rb