Module: NitroRails::NitroActionHelper

Defined in:
app/helpers/nitro_rails/nitro_action_helper.rb

Instance Method Summary collapse

Instance Method Details

#nitro_action_button(text, **options, &block) ⇒ Object



9
10
11
12
13
14
15
# File 'app/helpers/nitro_rails/nitro_action_helper.rb', line 9

def nitro_action_button(text, **options, &block)
  text ||= "Save" if !block_given?

  nitro_hidden_action_form(**options) do |form|
    form.submit(text, &block)
  end
end

#nitro_action_form(**options, &block) ⇒ Object



3
4
5
6
7
# File 'app/helpers/nitro_rails/nitro_action_helper.rb', line 3

def nitro_action_form(**options, &block)
  nitro_hidden_action_form(**options) do |form|
    form.submit_div(&block)
  end
end

#nitro_create(type, **options, &block) ⇒ Object

CREATE ##



28
29
30
31
# File 'app/helpers/nitro_rails/nitro_action_helper.rb', line 28

def nitro_create(type, **options, &block)
  nitro_create_options(type, options)
  nitro_action_form(**options, &block)
end

#nitro_create_button(text = nil, type, **options, &block) ⇒ Object



33
34
35
36
# File 'app/helpers/nitro_rails/nitro_action_helper.rb', line 33

def nitro_create_button(text=nil, type, **options, &block)
  nitro_create_options(type, options)
  nitro_action_button(text, **options, &block)
end

#nitro_create_options(type, options) ⇒ Object



38
39
40
41
42
43
44
# File 'app/helpers/nitro_rails/nitro_action_helper.rb', line 38

def nitro_create_options(type, options)
  nitro_options(options) do |options|
    options.default(:method, :post)
    options.default(:model, type.to_s.classify.constantize.new)
    options.default(:params, type.to_sym => options.delete(:params) || {})
  end
end

#nitro_destroy(resource = current_nitro_resource, **options, &block) ⇒ Object

DESTROY ##



98
99
100
101
# File 'app/helpers/nitro_rails/nitro_action_helper.rb', line 98

def nitro_destroy(resource = current_nitro_resource, **options, &block)
  nitro_destroy_options(resource, options)
  nitro_action_form(**options, &block)
end

#nitro_destroy_button(text_or_resource = nil, maybe_resource = current_nitro_resource, **options, &block) ⇒ Object



103
104
105
106
107
# File 'app/helpers/nitro_rails/nitro_action_helper.rb', line 103

def nitro_destroy_button(text_or_resource = nil, maybe_resource = current_nitro_resource, **options, &block)
  resource, text = button_args(text_or_resource, maybe_resource)
  nitro_destroy_options(resource, options)
  nitro_action_button(text, **options, &block)
end

#nitro_destroy_options(resource, options) ⇒ Object



109
110
111
112
113
114
# File 'app/helpers/nitro_rails/nitro_action_helper.rb', line 109

def nitro_destroy_options(resource, options)
  nitro_options(options) do |options|
    options.default(:method, :delete)
    options.default(:model, resource)
  end
end

#nitro_hidden_action_form(**options, &block) ⇒ Object



17
18
19
20
21
22
# File 'app/helpers/nitro_rails/nitro_action_helper.rb', line 17

def nitro_hidden_action_form(**options, &block)
  nitro_form(**options) do |form| 
    concat(form.generate_hidden_attribute_fields)
    concat(yield(form))
  end
end

#nitro_save(resource = current_nitro_resource, **options, &block) ⇒ Object

SAVE ##



50
51
52
53
# File 'app/helpers/nitro_rails/nitro_action_helper.rb', line 50

def nitro_save(resource = current_nitro_resource, **options, &block)
  nitro_save_options(resource, options)
  nitro_action_form(**options, &block)
end

#nitro_save_button(text_or_resource = nil, maybe_resource = current_nitro_resource, **options, &block) ⇒ Object

No idea if any of the buttons are working



56
57
58
59
60
# File 'app/helpers/nitro_rails/nitro_action_helper.rb', line 56

def nitro_save_button(text_or_resource = nil, maybe_resource = current_nitro_resource, **options, &block)
  resource, text = button_args(text_or_resource, maybe_resource)
  nitro_save_options(resource, options)
  nitro_action_button(text, **options, &block)
end

#nitro_save_options(resource, options) ⇒ Object



63
64
65
66
67
68
69
# File 'app/helpers/nitro_rails/nitro_action_helper.rb', line 63

def nitro_save_options(resource, options)
  nitro_options(options) do |options|
    options.default(:method, resource.persisted? ? :patch : :post)
    options.default(:model, resource)
    options.default(:params, resource.type.to_sym => options.delete(:params) || {})
  end
end

#nitro_update(resource = current_nitro_resource, **options, &block) ⇒ Object

UPDATE ##



75
76
77
78
# File 'app/helpers/nitro_rails/nitro_action_helper.rb', line 75

def nitro_update(resource = current_nitro_resource, **options, &block)
  nitro_update_options(resource, options)
  nitro_action_form(**options, &block)
end

#nitro_update_button(text_or_resource = nil, maybe_resource = current_nitro_resource, **options, &block) ⇒ Object



80
81
82
83
84
# File 'app/helpers/nitro_rails/nitro_action_helper.rb', line 80

def nitro_update_button(text_or_resource = nil, maybe_resource = current_nitro_resource, **options, &block)
  resource, text = button_args(text_or_resource, maybe_resource)
  nitro_update_options(resource, options)
  nitro_action_button(text, **options, &block)
end

#nitro_update_options(resource, options) ⇒ Object



86
87
88
89
90
91
92
# File 'app/helpers/nitro_rails/nitro_action_helper.rb', line 86

def nitro_update_options(resource, options)
  nitro_options(options) do |options|
    options.default(:method, :patch)
    options.default(:model, resource)
    options.default(:params, resource.type.to_sym => options.delete(:params) || {})
  end
end