Module: Compony::ModelMixin
- Extended by:
- ActiveSupport::Concern
- Included in:
- VirtualModel
- Defined in:
- lib/compony/model_mixin.rb
Instance Method Summary collapse
-
#feasibility_messages(action_name) ⇒ Object
Retrieves feasibility for the given instance and returns an array of reasons preventing the feasibility.
-
#feasible?(action_name, recompute: false) ⇒ Boolean
Retrieves feasibility for the given instance, returning a boolean indicating whether the action is feasibly.
-
#field(field_name, controller) ⇒ Object
Calls value_for on the desired field.
-
#full_feasibility_messages(action_name) ⇒ Object
Retrieves feasibility for the given instance and returns a string holding all reasons preventing the feasibility.
Instance Method Details
#feasibility_messages(action_name) ⇒ Object
Retrieves feasibility for the given instance and returns an array of reasons preventing the feasibility. Returns an empty array if feasible.
Conceptually, this is comparable to a model's errors.
181 182 183 184 185 |
# File 'lib/compony/model_mixin.rb', line 181 def (action_name) action_name = action_name.to_sym feasible?(action_name) if @feasibility_messages&.[](action_name).nil? # If feasibility check hasn't been performed yet for this action, perform it now return @feasibility_messages[action_name] end |
#feasible?(action_name, recompute: false) ⇒ Boolean
Retrieves feasibility for the given instance, returning a boolean indicating whether the action is feasibly. Calling this with an invalid action name will always return true. This also generates appropriate error messages for any reason causing it to return false. Feasilbility is cached, thus the second access will be faster.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/compony/model_mixin.rb', line 160 def feasible?(action_name, recompute: false) action_name = action_name.to_sym @feasibility_messages ||= {} # Abort if check has already run and recompute is false if @feasibility_messages[action_name].nil? || recompute # Lazily autodetect feasibilities self.class.autodetect_feasibilities! # Compute feasibility and gather messages @feasibility_messages[action_name] = [] feasibility_preventions[action_name]&.each do |prevention| if instance_exec(&prevention.block) @feasibility_messages[action_name] << prevention. end end end return @feasibility_messages[action_name].none? end |
#field(field_name, controller) ⇒ Object
Calls value_for on the desired field. Do not confuse with the static method field.
198 199 200 |
# File 'lib/compony/model_mixin.rb', line 198 def field(field_name, controller) fields[field_name.to_sym].value_for(self, controller:) end |
#full_feasibility_messages(action_name) ⇒ Object
Retrieves feasibility for the given instance and returns a string holding all reasons preventing the feasibility. Returns an empty string if feasible.
Messages are joined using commata. The first character is capitalized and a period is added to the end.
Conceptually, this is comparable to a model's full_messages.
191 192 193 194 195 |
# File 'lib/compony/model_mixin.rb', line 191 def (action_name) text = (action_name).join(', ').upcase_first text += '.' if text.present? return text end |