Module: Compony::ModelMixin

Extended by:
ActiveSupport::Concern
Included in:
VirtualModel
Defined in:
lib/compony/model_mixin.rb

Instance Method Summary collapse

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.

Parameters:

  • action_name (Symbol, String)

    the action that the feasibility should be checked for, e.g. :destroy



181
182
183
184
185
# File 'lib/compony/model_mixin.rb', line 181

def feasibility_messages(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.

Parameters:

  • action_name (Symbol, String)

    the action that the feasibility should be checked for, e.g. :destroy

  • recompute (Boolean) (defaults to: false)

    whether feasibility should be forcably recomputed even if a cached result is present

Returns:

  • (Boolean)


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.message
      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.

Parameters:

  • action_name (Symbol, String)

    the action that the feasibility should be checked for, e.g. :destroy



191
192
193
194
195
# File 'lib/compony/model_mixin.rb', line 191

def full_feasibility_messages(action_name)
  text = feasibility_messages(action_name).join(', ').upcase_first
  text += '.' if text.present?
  return text
end