Class: RailsStuff::Statusable::MappedHelper

Inherits:
Helper
  • Object
show all
Defined in:
lib/rails_stuff/statusable/mapped_helper.rb

Overview

Helper to hold

Instance Attribute Summary collapse

Attributes inherited from Helper

#field, #list, #model

Instance Method Summary collapse

Methods inherited from Helper

#attach, #define_class_method, #define_method, #define_scope, #translate

Constructor Details

#initializeMappedHelper

Returns a new instance of MappedHelper.



7
8
9
10
11
12
13
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 7

def initialize(*)
  super
  @mapping = @list
  @indifferent_mapping = mapping.with_indifferent_access
  @list = mapping.keys.freeze
  @inverse_mapping = mapping.invert.freeze
end

Instance Attribute Details

#indifferent_mappingObject (readonly)

Returns the value of attribute indifferent_mapping.



5
6
7
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 5

def indifferent_mapping
  @indifferent_mapping
end

#inverse_mappingObject (readonly)

Returns the value of attribute inverse_mapping.



5
6
7
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 5

def inverse_mapping
  @inverse_mapping
end

#mappingObject (readonly)

Returns the value of attribute mapping.



5
6
7
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 5

def mapping
  @mapping
end

Instance Method Details

#map(val) ⇒ Object



26
27
28
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 26

def map(val)
  map_with(indifferent_mapping, val)
end

#mapping_valuesObject



22
23
24
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 22

def mapping_values
  @mapping_values ||= mapping.values
end

#select_options(original: false, only: nil, except: nil) ⇒ Object



15
16
17
18
19
20
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 15

def select_options(original: false, only: nil, except: nil)
  return super(only: only, except: except) unless original
  only ||= mapping_values
  only -= except if except
  only.map { |x| [translate(inverse_mapping.fetch(x)), x] }
end

#unmap(val) ⇒ Object



30
31
32
# File 'lib/rails_stuff/statusable/mapped_helper.rb', line 30

def unmap(val)
  map_with(inverse_mapping, val)
end