Class: Wallaby::AllFields

Inherits:
Object
  • Object
show all
Defined in:
lib/fields/wallaby/all_fields.rb

Overview

A simple wrapper so that all fields (index/show/form and other fields appear before itself) can be set in one-line.

Examples:

set type for name to ‘’string’‘ in decorator

class ProductDecorator < ApplicationDecorator
  some_fields[:id][:sort_disabled] = true
  all_fields[:name][:type] = 'string'
  # index_fields[:name][:type] => 'string'
  # show_fields[:name][:type] => 'string'
  # form_fields[:name][:type] => 'string'
  # some_fields[:name][:type] => 'string'
end

Instance Method Summary collapse

Constructor Details

#initialize(decorator) ⇒ AllFields

Returns a new instance of AllFields.



17
18
19
20
# File 'lib/fields/wallaby/all_fields.rb', line 17

def initialize(decorator)
  @decorator = decorator
  @keys = []
end

Instance Method Details

#[](key) ⇒ AllFields

Returns self.

Parameters:

  • key (Symbol, String)

Returns:



24
25
26
27
# File 'lib/fields/wallaby/all_fields.rb', line 24

def [](key)
  @keys << key
  self
end

#[]=(last_key, value) ⇒ Object

Set value for given keys

Parameters:

  • last_key (Symbol, String)
  • value (Object)

Returns:

  • (Object)

    value



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fields/wallaby/all_fields.rb', line 33

def []=(last_key, value)
  all_fields.each do |fields_method|
    last = @keys.reduce(@decorator.try(fields_method)) do |, key|
      .try :[], key
    end
    last.try :[]=, last_key, value
  end

  @keys = []
  value # rubocop:disable Lint/Void
end