Class: RailsStuff::Statusable::Builder
- Inherits:
-
Object
- Object
- RailsStuff::Statusable::Builder
- Defined in:
- lib/rails_stuff/statusable/builder.rb
Overview
Basic builder for statuses list. Generates methods and scopes.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#helper ⇒ Object
readonly
Returns the value of attribute helper.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#suffix ⇒ Object
readonly
Returns the value of attribute suffix.
Instance Method Summary collapse
-
#each_status ⇒ Object
Yields every status with it’s database value into block.
- #field_accessor ⇒ Object
-
#field_reader ⇒ Object
Status as symbol.
-
#field_scope ⇒ Object
Scope with given status.
-
#field_writer ⇒ Object
Make field accept sympbols.
- #generate ⇒ Object
-
#initialize(helper, **options) ⇒ Builder
constructor
A new instance of Builder.
-
#status_method_name(status) ⇒ Object
Wraps status name with prefix and suffix.
- #translation_helpers ⇒ Object
-
#valid_list ⇒ Object
Field reader returns string, so we stringify list for validation.
- #validations ⇒ Object
-
#value_accessor(status, value) ⇒ Object
Generates methods for specific value.
-
#value_accessors ⇒ Object
Status accessors for every status.
-
#value_scopes ⇒ Object
Scopes for every status.
Constructor Details
#initialize(helper, **options) ⇒ Builder
Returns a new instance of Builder.
9 10 11 12 13 14 |
# File 'lib/rails_stuff/statusable/builder.rb', line 9 def initialize(helper, **) @helper = helper @options = @prefix = [:prefix] @suffix = [:suffix] end |
Instance Attribute Details
#helper ⇒ Object (readonly)
Returns the value of attribute helper.
5 6 7 |
# File 'lib/rails_stuff/statusable/builder.rb', line 5 def helper @helper end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/rails_stuff/statusable/builder.rb', line 5 def @options end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
5 6 7 |
# File 'lib/rails_stuff/statusable/builder.rb', line 5 def prefix @prefix end |
#suffix ⇒ Object (readonly)
Returns the value of attribute suffix.
5 6 7 |
# File 'lib/rails_stuff/statusable/builder.rb', line 5 def suffix @suffix end |
Instance Method Details
#each_status ⇒ Object
Yields every status with it’s database value into block.
36 37 38 |
# File 'lib/rails_stuff/statusable/builder.rb', line 36 def each_status list.each { |x| yield x, x.to_s } end |
#field_accessor ⇒ Object
83 84 85 86 |
# File 'lib/rails_stuff/statusable/builder.rb', line 83 def field_accessor field_reader field_writer end |
#field_reader ⇒ Object
Status as symbol.
97 98 99 100 101 102 103 |
# File 'lib/rails_stuff/statusable/builder.rb', line 97 def field_reader field = self.field define_method "#{field}_sym" do val = self[field] val && val.to_sym end end |
#field_scope ⇒ Object
Scope with given status. Useful for has_scope.
46 47 48 49 |
# File 'lib/rails_stuff/statusable/builder.rb', line 46 def field_scope field = self.field define_scope "with_#{field}", ->(status) { where(field => status) } end |
#field_writer ⇒ Object
Make field accept sympbols.
89 90 91 92 93 94 |
# File 'lib/rails_stuff/statusable/builder.rb', line 89 def field_writer define_method "#{field}=" do |val| val = val.to_s if val.is_a?(Symbol) super(val) end end |
#generate ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/rails_stuff/statusable/builder.rb', line 16 def generate validations if .fetch(:validate, true) field_accessor field_scope value_scopes value_accessors translation_helpers end |
#status_method_name(status) ⇒ Object
Wraps status name with prefix and suffix.
41 42 43 |
# File 'lib/rails_stuff/statusable/builder.rb', line 41 def status_method_name(status) "#{prefix}#{status}#{suffix}" end |
#translation_helpers ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/rails_stuff/statusable/builder.rb', line 105 def translation_helpers field = self.field define_method "#{field}_name" do val = send(field) self.class.t(".#{field}_name.#{val}") if val end end |
#valid_list ⇒ Object
Field reader returns string, so we stringify list for validation.
31 32 33 |
# File 'lib/rails_stuff/statusable/builder.rb', line 31 def valid_list list.map(&:to_s) end |
#validations ⇒ Object
25 26 27 28 |
# File 'lib/rails_stuff/statusable/builder.rb', line 25 def validations model.validates_inclusion_of field, {in: valid_list}.merge!(.fetch(:validate, {})) end |
#value_accessor(status, value) ⇒ Object
Generates methods for specific value.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rails_stuff/statusable/builder.rb', line 68 def value_accessor(status, value) field = self.field # Shortcut to check status. define_method "#{status_method_name(status)}?" do # Access raw value, 'cause reader can be overriden. self[field] == value end # Shortcut to update status. define_method "#{status_method_name(status)}!" do update!(field => value) end end |
#value_accessors ⇒ Object
Status accessors for every status.
52 53 54 55 56 |
# File 'lib/rails_stuff/statusable/builder.rb', line 52 def value_accessors each_status do |status, value| value_accessor status, value end end |
#value_scopes ⇒ Object
Scopes for every status.
59 60 61 62 63 64 65 |
# File 'lib/rails_stuff/statusable/builder.rb', line 59 def value_scopes field = self.field each_status do |status, value| define_scope status_method_name(status), -> { where(field => value) } define_scope "not_#{status_method_name(status)}", -> { where.not(field => value) } end end |