Class: RailsStuff::Statusable::Builder
- Inherits:
-
Object
- Object
- RailsStuff::Statusable::Builder
- Defined in:
- lib/rails_stuff/statusable.rb
Overview
Generates methods and scopes.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#field ⇒ Object
readonly
Returns the value of attribute field.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#statuses ⇒ Object
(also: #statuses_list)
readonly
Returns the value of attribute statuses.
-
#suffix ⇒ Object
readonly
Returns the value of attribute suffix.
Instance Method Summary collapse
- #define_method(method, &block) ⇒ Object
-
#define_scope(name, body) ⇒ Object
Rails 4 doesn’t use ‘instance_exec` for scopes, so we do it manually.
-
#field_reader ⇒ Object
Status as symbol.
-
#field_scope ⇒ Object
Scope with given status.
-
#field_writer ⇒ Object
Make field accept sympbols.
- #generate ⇒ Object
- #generate_class_method(method, &block) ⇒ Object
-
#initialize(model, field, statuses, **options) ⇒ Builder
constructor
A new instance of Builder.
- #select_options_helper ⇒ Object
-
#status_accessor(status_name, value) ⇒ Object
Generates methods for specific value.
- #translation_helpers ⇒ Object
- #validations ⇒ Object
- #value_methods ⇒ Object
Constructor Details
#initialize(model, field, statuses, **options) ⇒ Builder
Returns a new instance of Builder.
64 65 66 67 68 69 70 71 |
# File 'lib/rails_stuff/statusable.rb', line 64 def initialize(model, field, statuses, **) @model = model @field = field @statuses = statuses = @prefix = [:prefix] @suffix = [:suffix] end |
Instance Attribute Details
#field ⇒ Object (readonly)
Returns the value of attribute field.
61 62 63 |
# File 'lib/rails_stuff/statusable.rb', line 61 def field @field end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
61 62 63 |
# File 'lib/rails_stuff/statusable.rb', line 61 def model @model end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
61 62 63 |
# File 'lib/rails_stuff/statusable.rb', line 61 def end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
61 62 63 |
# File 'lib/rails_stuff/statusable.rb', line 61 def prefix @prefix end |
#statuses ⇒ Object (readonly) Also known as: statuses_list
Returns the value of attribute statuses.
61 62 63 |
# File 'lib/rails_stuff/statusable.rb', line 61 def statuses @statuses end |
#suffix ⇒ Object (readonly)
Returns the value of attribute suffix.
61 62 63 |
# File 'lib/rails_stuff/statusable.rb', line 61 def suffix @suffix end |
Instance Method Details
#define_method(method, &block) ⇒ Object
171 172 173 |
# File 'lib/rails_stuff/statusable.rb', line 171 def define_method(method, &block) model.statusable_methods.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.
165 166 167 168 169 |
# File 'lib/rails_stuff/statusable.rb', line 165 def define_scope(name, body) model.singleton_class.send(:define_method, name) do |*args| all.scoping { instance_exec(*args, &body) } || all end end |
#field_reader ⇒ Object
Status as symbol.
130 131 132 133 134 135 136 |
# File 'lib/rails_stuff/statusable.rb', line 130 def field_reader field = self.field define_method "#{field}_sym" do val = send(field) val && val.to_sym end end |
#field_scope ⇒ Object
Scope with given status. Useful for has_scope.
89 90 91 92 |
# File 'lib/rails_stuff/statusable.rb', line 89 def field_scope field = self.field define_scope "with_#{field}", ->(status) { where(field => status) } end |
#field_writer ⇒ Object
Make field accept sympbols.
122 123 124 125 126 127 |
# File 'lib/rails_stuff/statusable.rb', line 122 def field_writer define_method "#{field}=" do |val| val = val.to_s if val.is_a?(Symbol) super(val) end end |
#generate ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/rails_stuff/statusable.rb', line 73 def generate validations unless [:validate] == false field_reader field_writer translation_helpers field_scope value_methods end |
#generate_class_method(method, &block) ⇒ Object
175 176 177 |
# File 'lib/rails_stuff/statusable.rb', line 175 def generate_class_method(method, &block) model.statusable_methods::ClassMethods.send(:define_method, method, &block) end |
#select_options_helper ⇒ Object
154 155 156 157 158 159 160 161 162 |
# File 'lib/rails_stuff/statusable.rb', line 154 def statuses_list = self.statuses_list translation_method = :"#{field}_name" # Returns array compatible with select_options helper. generate_class_method "#{field}_select_options" do |args = {}| filtered_statuses = statuses_list - Array.wrap(args[:except]) filtered_statuses.map { |x| [send(translation_method, x), x] } end end |
#status_accessor(status_name, value) ⇒ Object
Generates methods for specific value.
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rails_stuff/statusable.rb', line 107 def status_accessor(status_name, value) field = self.field # Shortcut to check status. define_method "#{prefix}#{status_name}#{suffix}?" do self[field] == value end # Shortcut to update status. define_method "#{prefix}#{status_name}#{suffix}!" do update_attributes!(field => value) end end |
#translation_helpers ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rails_stuff/statusable.rb', line 138 def translation_helpers field = self.field sym_method = "#{field}_sym" # Class-level translation helper. generate_class_method "#{field}_name" do |status| t(".#{field}_name.#{status}") if status end # Translation helper. define_method "#{field}_name" do val = send(sym_method) self.class.t(".#{field}_name.#{val}") if val end end |
#validations ⇒ Object
83 84 85 86 |
# File 'lib/rails_stuff/statusable.rb', line 83 def validations model.validates_inclusion_of field, {in: statuses.map(&:to_s)}.merge!(.fetch(:validate, {})) end |
#value_methods ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rails_stuff/statusable.rb', line 94 def value_methods field = self.field statuses.map(&:to_s).each do |status_name| # Scopes for every status. define_scope "#{prefix}#{status_name}#{suffix}", -> { where(field => status_name) } define_scope "not_#{prefix}#{status_name}#{suffix}", -> { where.not(field => status_name) } status_accessor status_name, status_name end end |