Class: Golden::ActionFormOperator

Inherits:
ApplicationOperator show all
Extended by:
ActiveModel::Callbacks
Defined in:
lib/golden/objects/form/action_form_operator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DatepickerConcern

#filter_dates_of_datepicker, #filter_duration_of_datepicker

Constructor Details

#initialize(params, accessors = {}) ⇒ ActionFormOperator

Returns a new instance of ActionFormOperator.



39
40
41
42
43
44
# File 'lib/golden/objects/form/action_form_operator.rb', line 39

def initialize(params, accessors = {})
  @params = params
  @action = params[:action]&.to_sym
  assign_attributes(accessors || {})
  run_callbacks(:build_form) { @form ||= build_form }
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



30
31
32
# File 'lib/golden/objects/form/action_form_operator.rb', line 30

def action
  @action
end

#formObject

Returns the value of attribute form.



29
30
31
# File 'lib/golden/objects/form/action_form_operator.rb', line 29

def form
  @form
end

#paramsObject

Returns the value of attribute params.



30
31
32
# File 'lib/golden/objects/form/action_form_operator.rb', line 30

def params
  @params
end

#userObject

Returns the value of attribute user.



29
30
31
# File 'lib/golden/objects/form/action_form_operator.rb', line 29

def user
  @user
end

Class Method Details

.has_actions(actions, form: nil, presenter: nil) ⇒ Object

rubocop:disable Naming/PredicateName



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/golden/objects/form/action_form_operator.rb', line 7

def has_actions(actions, form: nil, presenter: nil)
  define_method :actions do
    actions
  end

  define_method :form_class do
    return form.to_s.constantize if form.present?

    "#{action.capitalize}Form".constantize
  end

  define_method :presenter_class do
    return presenter.to_s.constantize if presenter.present?

    "#{action.capitalize}FormPresenter".constantize
  end
end

Instance Method Details

#form_presenterObject



59
60
61
# File 'lib/golden/objects/form/action_form_operator.rb', line 59

def form_presenter
  @form_presenter ||= presenter_class.new(form)
end

#performObject



52
53
54
55
56
57
# File 'lib/golden/objects/form/action_form_operator.rb', line 52

def perform
  return if invalid?

  run_callbacks(:save_form) { save_form }
  errors.empty?
end

#valid?(context = nil) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/golden/objects/form/action_form_operator.rb', line 46

def valid?(context = nil)
  super
  run_callbacks(:validate_form) { validate_form }
  errors.empty?
end