Class: HasHelpers::Resource::FormPresenter

Inherits:
BasePresenter show all
Includes:
HasForms::FormBuilder
Defined in:
app/presenters/has_helpers/resource/form_presenter.rb

Direct Known Subclasses

InfoPresenter

Instance Attribute Summary collapse

Attributes inherited from BasePresenter

#errors, #wrapped_object

Instance Method Summary collapse

Methods inherited from BasePresenter

#==, #error_on, #initialize, wrap, wrap_with_options

Methods included from Attributes

included

Constructor Details

This class inherits a constructor from HasHelpers::BasePresenter

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class HasHelpers::BasePresenter

Instance Attribute Details

#current_organizationObject



56
57
58
# File 'app/presenters/has_helpers/resource/form_presenter.rb', line 56

def current_organization
  @current_organization || (controller && controller.current_organization)
end

#current_userObject



52
53
54
# File 'app/presenters/has_helpers/resource/form_presenter.rb', line 52

def current_user
  @current_user || (controller && controller.current_user)
end

#paramsObject



48
49
50
# File 'app/presenters/has_helpers/resource/form_presenter.rb', line 48

def params
  @params || (controller && controller.params)
end

Instance Method Details

#authorize_active_element_procObject



60
61
62
63
64
65
66
67
68
69
# File 'app/presenters/has_helpers/resource/form_presenter.rb', line 60

def authorize_active_element_proc
  @authorize_active_element_proc ||=
    ->(fieldset) do
      if fieldset.respond_to?(:legend) && fieldset.legend.present?
        controller.current_ability.can?(:read, fieldset.legend)
      else
        true
      end
    end
end

#express_form(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/presenters/has_helpers/resource/form_presenter.rb', line 7

def express_form(*args)
  if wrapped_object.express_step.nil? && wrapped_object.new_record?
    wrapped_object.express_step = "basic_info"
  elsif wrapped_object.express_step != "basic_info" && wrapped_object.new_record?
    wrapped_object.express_step = "basic_info"
    wrapped_object.errors.add(:express_step, "You must complete the first step to continue!")
  end

  options = args.extract_options!

  resource = wrapped_object.class.name.demodulize.underscore
  cancel_service =
    if wrapped_object.new_record?
      routes.send("new_#{ resource }_path")
    else
      routes.send("edit_#{ resource }_path", wrapped_object) << "/#{ wrapped_object.express_step }"
    end

  options.merge!(
    legend: wrapped_object.express_step.titleize,
    cancel_service: cancel_service,
    submits: [
      save_and_continue_options,
      { name: "save_&_finish", value: "Save & Finish", skin: "btn btn-default" }
    ]
  )

  form_builder(wrapped_object, options) do |form|
    form.hidden_field :express_step, value: wrapped_object.express_step.html_safe # rubocop:disable Rails/OutputSafety

    next_express_step = express_form_steps(form)

    if next_express_step
      form.hidden_field :next_express_step, prefix: "", value: next_express_step.html_safe # rubocop:disable Rails/OutputSafety
      unless wrapped_object.new_record?
        form.element.links << { caption: "Next Step", service: "#{form.element.service}/edit/#{ URI.parser.escape(next_express_step.to_s) }" }
      end
    end
  end
end