Module: Compony::ModelMixin

Extended by:
ActiveSupport::Concern
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



92
93
94
95
96
# File 'lib/compony/model_mixin.rb', line 92

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)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/compony/model_mixin.rb', line 71

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.



109
110
111
# File 'lib/compony/model_mixin.rb', line 109

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



102
103
104
105
106
# File 'lib/compony/model_mixin.rb', line 102

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