button_to_form 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 98adc411f0fb0e2dfc0db591e3bafa70ca12e7a33ba230c63b25036b4430350c
4
- data.tar.gz: c91c4b77327bec023a76a11b4ab4e2f3c39e1d99894b4e303fde214933a76fda
3
+ metadata.gz: fc4b4ecc124a1394c6a8f02525a0d88790f83f032393818ff15a4f9488adb7ad
4
+ data.tar.gz: cb7fca9a94e3285e539c4c35df78e1919dce9e6cc54be85129fc6bbf7c5c7cd5
5
5
  SHA512:
6
- metadata.gz: db2c812e441ffc139591f40fb0fffdac22a6c9700fd130fa4f3ec5c51f95a33429e11293f50275178e0b4f6259cd093dda34b7ca69184a55f43c57fa4e617126
7
- data.tar.gz: df6e59e461b0a57c731db35c5fce1c97eec4a896f83e4d6637f8d922e1f955c09416b8350476200b98d9f757870cab52d1e92d9fa1ae3eeb62fb992322a28afd
6
+ metadata.gz: 1bb3ed8a7d0dda063f6871d1ef36dcea0e00c1922b8be3cf83f09eb857f3954c8cc82e2653f3fb31fa0534d5f9b6c0a6c82a2cdecac12b8fce23175ea6fba379
7
+ data.tar.gz: adbbf8e3c6f2ff3031d40528d5353023937863d74e3c26c15ffe756782a5db85acb38bbf14f24adc613d52761ab3be827ad4d7b4366fb36493848ddb9c9f155f
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ This project follows [semver 2.0.0](http://semver.org/spec/v2.0.0.html) and the
4
+ recommendations of [keepachangelog.com](http://keepachangelog.com/).
5
+
6
+ ## 0.2.0 (2019-12-12)
7
+
8
+ - Make `button_options` optional
9
+ - Don't output another (duplicate) `form` with same id if called again with the same form id
10
+
11
+ ## 0.1.0 (2019-12-11)
12
+
13
+ Initial release
data/Readme.md CHANGED
@@ -1,16 +1,17 @@
1
1
  # `button_to_form`
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/button_to_form.svg)](https://badge.fury.io/rb/button_to_form)
4
+ [![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](https://rdoc.info/github/TylerRick/button_to_form/master)
4
5
 
5
6
  ## Motivation
6
7
 
7
8
  The [button_to](https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to)
8
- provided by rails doesn't work when used inside of a form tag, because it adds a new <form> and
9
- HTML doesn't allow a form within a form.
9
+ provided by rails _doesn't work_ when used inside of a `<form>` tag, because it adds a new `<form>`
10
+ (at your current nesting level) and HTML doesn't allow a `form` to be nested within another `form`.
10
11
 
11
12
  ## How does it work?
12
13
 
13
- The HTML spec does, however, allow a button/input/etc. to be
14
+ The HTML spec _does_, however, allow a button/input/etc. to be
14
15
  associated with a different form than the one it is nested within.
15
16
 
16
17
  The [HTML spec says](https://html.spec.whatwg.org/#association-of-controls-and-forms):
@@ -19,9 +20,39 @@ The [HTML spec says](https://html.spec.whatwg.org/#association-of-controls-and-f
19
20
  > (as described below), but, if it is listed, may have a form attribute specified to override
20
21
  > this.
21
22
 
22
- This helper takes advantage of that, rendering a separate, empty form in the footer, and then
23
- associating this button with it, so that it submits with *that* form's action rather than the
24
- action of the form it is a descendant of.
23
+ This helper takes advantage of that, rendering a separate, empty `<form>` in the footer, and then
24
+ associating this `button` with it, so that it submits to *that* `form`'s action _rather_ than to the
25
+ action of the `form` it is a descendant of.
26
+
27
+ So — assuming you have added `<%= content_for(:footer) %>` somewhere in your layout — this source:
28
+
29
+ ```ruby
30
+ = form_tag '/main_form' do
31
+ = hidden_field_tag :main_form_param_1, 'main_form'
32
+ = text_field_tag :main_form_param_2, ''
33
+
34
+ = button_to_form 'Make happy', '/make_happy' do
35
+ = hidden_field_tag :how_happy, 'ecstatic!'
36
+ ```
37
+
38
+ will get rendered to HTML that looks something like this:
39
+ ```html
40
+ <form action="/main_form" accept-charset="UTF-8" method="post">
41
+ <input type="hidden" name="main_form_param_1" id="main_form_param_1" value="main_form">
42
+ <input type="text" name="main_form_param_2" id="main_form_param_2" value="">
43
+ <button name="button" type="submit" form="form-1e7dc01b-46d0-4a44-908e-77fbe2a7ec98">Make happy</button>
44
+ </form>
45
+
46
+ <div id="footer">
47
+ <form id="form-1e7dc01b-46d0-4a44-908e-77fbe2a7ec98" action="/make_happy" accept-charset="UTF-8" method="post">
48
+ <input type="hidden" name="how_happy" id="how_happy" value="ecstatic!">
49
+ </form>
50
+ </div>
51
+ ```
52
+
53
+ As you can see, there are 2 `<form>` tags that get rendered, but neither of them is nested within
54
+ the other, so it is allowed — and works well! Now you can include `button_to` calls inside of
55
+ other forms as much as your heart desires.
25
56
 
26
57
 
27
58
  ## Installation
@@ -37,37 +68,53 @@ For this helper to work, you *must* also include this somewhere in your layout:
37
68
  <%= content_for(:footer) %>
38
69
  ```
39
70
 
71
+ If you want these forms to be added to a different `content_for` key, say `:forms`, you can
72
+ configure it like so:
73
+ ```ruby
40
74
  ButtonToFormHelper.content_for_name = :forms
75
+ ```
41
76
 
42
77
  ## Usage
43
78
 
44
79
  By default, it will generate a unique id for the form. In its simplest form, it can be used as a
45
80
  drop-in replacement for Rails's `button_to`. Example:
46
81
 
82
+ ```ruby
47
83
  = button_to_form 'Make happy', [:make_happy, user]
84
+ ```
85
+ (These examples use HAML, but you could just as easily use ERB.)
86
+
87
+ Unless you have a use case where the default generated id doesn't work, it is recommended to use
88
+ that approach, as it ensures that each `button_to_form` call has its own corresponding unique
89
+ `<form>`.
48
90
 
49
- If you need to reference this form in other places, you should specify the form's id. You can
50
- also pass other `form_options`, such as `method`.
91
+ If, however, you need to reference this form in other places, you can specify a well-known id as the
92
+ form's id. You can also pass other `form_options`, such as `method`.
51
93
 
94
+ ```ruby
52
95
  = button_to_form 'Delete', thing,
53
96
  {data: {confirm: 'Are you sure?'}},
54
97
  {method: 'delete', id: 'delete_thing_form'}
55
98
  = hidden_field_tag :some_id, some_id, {form: 'delete_thing_form'}
99
+ ```
56
100
 
57
- You may pass along additional data to the endpoint via hidden_field_tags by passing them as a
58
- block. You don't need to specify the form in this case because, as descendants, they are already
59
- associated with this form.
101
+ You may pass along additional data to the endpoint via `hidden_field_tag`s by passing them inside a
102
+ block. You don't need to specify the `form` in this case because, as descendants, they are already
103
+ associated with this new form.
60
104
 
105
+ ```ruby
61
106
  = button_to_form 'Delete', thing,
62
107
  {data: {confirm: 'Are you sure?'}},
63
- {method: 'delete'}
108
+ {method: 'delete'} do
64
109
  = hidden_field_tag :some_id, some_id
65
-
110
+ ```
66
111
 
67
112
  ## Development
68
113
 
69
114
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
115
 
116
+ To start up a development web server with the same internal Rails app that is used for tests, run `rackup`.
117
+
71
118
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
72
119
 
73
120
  ## Contributing
@@ -41,19 +41,25 @@ module ButtonToFormHelper
41
41
  # {method: 'delete'}
42
42
  # = hidden_field_tag :some_id, some_id
43
43
  #
44
- def button_to_form(button_text, url, button_options, form_options = {}, &block)
44
+ # If you call button_to_form with the same form id, it will render the button but will _not_
45
+ # render another form with the same id since it doesn't make sense to have multiple forms with the
46
+ # same id. Instead it will assume you want another form that reuses (gets associated with) the
47
+ # *existing* form with that id.
48
+ #
49
+ def button_to_form(button_text, url, button_options = {}, form_options = {}, &block)
45
50
  form_options[:id] ||= "form-#{SecureRandom.uuid}"
46
- content_for(ButtonToFormHelper.content_for_name) do
47
- # @button_to_form_rendered_content_for_name = true
48
- # controller.instance_variable_set '@button_to_form_rendered_content_for_name', true
49
- form_tag(url, **form_options) do
50
- block.call if block
51
+ @button_to_form_ids ||= []
52
+ @button_to_form_id = form_options[:id]
53
+ unless @button_to_form_ids.include?(@button_to_form_id)
54
+ content_for(ButtonToFormHelper.content_for_name) do
55
+ form_tag(url, **form_options) do
56
+ block.call if block
57
+ end
51
58
  end
52
59
  end
53
- # @button_to_form_needs_to_render_content_for_name = true
54
- # controller.instance_variable_set '@button_to_form_needs_to_render_content_for_name', true
60
+ @button_to_form_ids << @button_to_form_id
55
61
 
56
- button_tag(button_text, **button_options, type: 'submit', form: form_options[:id])
62
+ button_tag(button_text, **button_options, type: 'submit', form: @button_to_form_id)
57
63
  end
58
64
 
59
65
  end
data/config.ru CHANGED
@@ -1,9 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "rubygems"
4
- require "bundler"
3
+ require_relative './config/rails_environment'
5
4
 
6
- Bundler.require :default, :development
7
-
8
- Combustion.initialize! :action_controller, :action_view
9
5
  run Combustion::Application
@@ -0,0 +1,11 @@
1
+ # This common code has been extracted to ensure that both config.ru and spec/spec_helper.rb
2
+ # configure Combustion/Rails in exactly the same way.
3
+
4
+ require "rubygems"
5
+ require "bundler"
6
+
7
+ Bundler.require :default, :development
8
+
9
+ Combustion.initialize! :action_controller, :action_view
10
+
11
+ ButtonToFormHelper.content_for_name = :forms
@@ -1,3 +1,3 @@
1
1
  module ButtonToForm
2
- Version = "0.1.0"
2
+ Version = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: button_to_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Rick
@@ -59,6 +59,7 @@ files:
59
59
  - bin/setup
60
60
  - button_to_form.gemspec
61
61
  - config.ru
62
+ - config/rails_environment.rb
62
63
  - lib/button_to_form.rb
63
64
  - lib/button_to_form/engine.rb
64
65
  - lib/button_to_form/version.rb