Class: RailsStuff::Statusable::Helper
- Inherits:
-
Object
- Object
- RailsStuff::Statusable::Helper
- Defined in:
- lib/rails_stuff/statusable/helper.rb
Overview
Class to hold helper methods for statusable field.
Order.has_status_field :status, %i(pending complete)
Order.statuses.list # => %(pending complete)
# ...
Direct Known Subclasses
Instance Attribute Summary collapse
-
#field ⇒ Object
readonly
Returns the value of attribute field.
-
#list ⇒ Object
readonly
Returns the value of attribute list.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
-
#attach(method_name = field.to_s.pluralize) ⇒ Object
Generate class method in model to access helper.
- #define_class_method(method, &block) ⇒ Object
- #define_method(method, &block) ⇒ Object
-
#define_scope(name, body) ⇒ Object
Rails 4 doesn’t use ‘instance_exec` for scopes, so we do it manually.
-
#initialize(model, field, statuses) ⇒ Helper
constructor
A new instance of Helper.
-
#select_options(only: nil, except: nil) ⇒ Object
Returns array compatible with select_options helper.
- #translate(status) ⇒ Object (also: #t)
Constructor Details
#initialize(model, field, statuses) ⇒ Helper
Returns a new instance of Helper.
11 12 13 14 15 |
# File 'lib/rails_stuff/statusable/helper.rb', line 11 def initialize(model, field, statuses) @model = model @field = field.freeze @list = statuses.freeze end |
Instance Attribute Details
#field ⇒ Object (readonly)
Returns the value of attribute field.
9 10 11 |
# File 'lib/rails_stuff/statusable/helper.rb', line 9 def field @field end |
#list ⇒ Object (readonly)
Returns the value of attribute list.
9 10 11 |
# File 'lib/rails_stuff/statusable/helper.rb', line 9 def list @list end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
9 10 11 |
# File 'lib/rails_stuff/statusable/helper.rb', line 9 def model @model end |
Instance Method Details
#attach(method_name = field.to_s.pluralize) ⇒ Object
Generate class method in model to access helper.
31 32 33 34 |
# File 'lib/rails_stuff/statusable/helper.rb', line 31 def attach(method_name = field.to_s.pluralize) helper = self define_class_method(method_name) { helper } end |
#define_class_method(method, &block) ⇒ Object
52 53 54 |
# File 'lib/rails_stuff/statusable/helper.rb', line 52 def define_class_method(method, &block) methods_module::ClassMethods.send(:define_method, method, &block) end |
#define_method(method, &block) ⇒ Object
48 49 50 |
# File 'lib/rails_stuff/statusable/helper.rb', line 48 def define_method(method, &block) methods_module.send(:define_method, method, &block) end |
#define_scope(name, body) ⇒ Object
Rails 4 doesn’t use ‘instance_exec` for scopes, so we do it manually. For Rails 5 it’s just use ‘.scope`.
38 39 40 41 42 43 44 45 46 |
# File 'lib/rails_stuff/statusable/helper.rb', line 38 def define_scope(name, body) if RailsStuff.rails4? model.singleton_class.send(:define_method, name) do |*args| all.scoping { instance_exec(*args, &body) } || all end else model.scope(name, body) end end |
#select_options(only: nil, except: nil) ⇒ Object
Returns array compatible with select_options helper.
24 25 26 27 28 |
# File 'lib/rails_stuff/statusable/helper.rb', line 24 def (only: nil, except: nil) only ||= list only -= except if except only.map { |x| [translate(x), x] } end |
#translate(status) ⇒ Object Also known as: t
17 18 19 |
# File 'lib/rails_stuff/statusable/helper.rb', line 17 def translate(status) model.t(".#{field}_name.#{status}") if status end |