button_to_form 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 98adc411f0fb0e2dfc0db591e3bafa70ca12e7a33ba230c63b25036b4430350c
4
+ data.tar.gz: c91c4b77327bec023a76a11b4ab4e2f3c39e1d99894b4e303fde214933a76fda
5
+ SHA512:
6
+ metadata.gz: db2c812e441ffc139591f40fb0fffdac22a6c9700fd130fa4f3ec5c51f95a33429e11293f50275178e0b4f6259cd093dda34b7ca69184a55f43c57fa4e617126
7
+ data.tar.gz: df6e59e461b0a57c731db35c5fce1c97eec4a896f83e4d6637f8d922e1f955c09416b8350476200b98d9f757870cab52d1e92d9fa1ae3eeb62fb992322a28afd
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 1.17.3
data/Changelog.md ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in button_to_form.gemspec
6
+ gemspec
7
+
8
+ gem "bundler", "~> 1.17"
9
+ gem "byebug"
10
+ gem "capybara", '3.23.0'
11
+ gem "combustion", "~> 1.0"
12
+ gem "haml-rails"
13
+ gem "pry"
14
+ gem "rails", ">= 4.2"
15
+ gem "rake", "~> 10.0"
16
+ gem "rspec-rails", "~> 3.0"
17
+ gem 'selenium-webdriver'
data/License ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2019 Tyler Rick
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
4
+ associated documentation files (the "Software"), to deal in the Software without restriction,
5
+ including without limitation the rights to use, copy, modify, merge, publish, distribute,
6
+ sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
7
+ furnished to do so, subject to the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or substantial
10
+ portions of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/Readme.md ADDED
@@ -0,0 +1,75 @@
1
+ # `button_to_form`
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/button_to_form.svg)](https://badge.fury.io/rb/button_to_form)
4
+
5
+ ## Motivation
6
+
7
+ 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.
10
+
11
+ ## How does it work?
12
+
13
+ The HTML spec does, however, allow a button/input/etc. to be
14
+ associated with a different form than the one it is nested within.
15
+
16
+ The [HTML spec says](https://html.spec.whatwg.org/#association-of-controls-and-forms):
17
+
18
+ > A form-associated element is, by default, associated with its nearest ancestor form element
19
+ > (as described below), but, if it is listed, may have a form attribute specified to override
20
+ > this.
21
+
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.
25
+
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application's `Gemfile`:
30
+
31
+ ```ruby
32
+ gem 'button_to_form'
33
+ ```
34
+
35
+ For this helper to work, you *must* also include this somewhere in your layout:
36
+ ```ruby
37
+ <%= content_for(:footer) %>
38
+ ```
39
+
40
+ ButtonToFormHelper.content_for_name = :forms
41
+
42
+ ## Usage
43
+
44
+ By default, it will generate a unique id for the form. In its simplest form, it can be used as a
45
+ drop-in replacement for Rails's `button_to`. Example:
46
+
47
+ = button_to_form 'Make happy', [:make_happy, user]
48
+
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`.
51
+
52
+ = button_to_form 'Delete', thing,
53
+ {data: {confirm: 'Are you sure?'}},
54
+ {method: 'delete', id: 'delete_thing_form'}
55
+ = hidden_field_tag :some_id, some_id, {form: 'delete_thing_form'}
56
+
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.
60
+
61
+ = button_to_form 'Delete', thing,
62
+ {data: {confirm: 'Are you sure?'}},
63
+ {method: 'delete'}
64
+ = hidden_field_tag :some_id, some_id
65
+
66
+
67
+ ## Development
68
+
69
+ 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
+
71
+ 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
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/TylerRick/button_to_form.
@@ -0,0 +1,59 @@
1
+ module ButtonToFormHelper
2
+ class << self
3
+ attr_accessor :content_for_name
4
+ end
5
+ self.content_for_name = :footer
6
+
7
+ # 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. It does, however, allow a button/input/etc. to be
10
+ # associated with a different form than the one it is nested within.
11
+ #
12
+ # The [HTML spec says](https://html.spec.whatwg.org/#association-of-controls-and-forms):
13
+ #
14
+ # > A form-associated element is, by default, associated with its nearest ancestor form element
15
+ # > (as described below), but, if it is listed, may have a form attribute specified to override
16
+ # > this.
17
+ #
18
+ # This helper takes advantage of that, rendering a separate, empty form in the footer, and then
19
+ # associating this button with it, so that it submits with *that* form's action rather than the
20
+ # action of the form it is a descendant of.
21
+ #
22
+ # By default, it will generate a unique id for the form. In its simplest form, it can be used as a
23
+ # drop-in replacement for Rails's `button_to`. Example:
24
+ #
25
+ # = button_to_form 'Make happy', [:make_happy, user]
26
+ #
27
+ # If you need to reference this form in other places, you should specify the form's id. You can
28
+ # also pass other `form_options`, such as `method`.
29
+ #
30
+ # = button_to_form 'Delete', thing,
31
+ # {data: {confirm: 'Are you sure?'}},
32
+ # {method: 'delete', id: 'delete_thing_form'}
33
+ # = hidden_field_tag :some_id, some_id, {form: 'delete_thing_form'}
34
+ #
35
+ # You may pass along additional data to the endpoint via hidden_field_tags by passing them as a
36
+ # block. You don't need to specify the form in this case because, as descendants, they are already
37
+ # associated with this form.
38
+ #
39
+ # = button_to_form 'Delete', thing,
40
+ # {data: {confirm: 'Are you sure?'}},
41
+ # {method: 'delete'}
42
+ # = hidden_field_tag :some_id, some_id
43
+ #
44
+ def button_to_form(button_text, url, button_options, form_options = {}, &block)
45
+ 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
+ end
52
+ 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
55
+
56
+ button_tag(button_text, **button_options, type: 'submit', form: form_options[:id])
57
+ end
58
+
59
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "button_to_form"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "button_to_form/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "button_to_form"
8
+ spec.version = ButtonToForm::Version
9
+ spec.authors = ["Tyler Rick"]
10
+ spec.email = ["tyler@tylerrick.com"]
11
+
12
+ spec.summary = %q{A button_to helper that can be used inside a form tag (unlike Rails's own button_to)}
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/TylerRick/button_to_form"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "#{spec.metadata["source_code_uri"]}/blob/master/Changelog.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.required_ruby_version = ">= 2.3.0"
30
+ spec.add_dependency "actionpack", ">= 4.2"
31
+ spec.add_dependency "railties", ">= 4.2"
32
+ end
data/config.ru ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubygems"
4
+ require "bundler"
5
+
6
+ Bundler.require :default, :development
7
+
8
+ Combustion.initialize! :action_controller, :action_view
9
+ run Combustion::Application
@@ -0,0 +1,2 @@
1
+ require "button_to_form/version"
2
+ require "button_to_form/engine"
@@ -0,0 +1,28 @@
1
+ require 'rails/engine'
2
+ require 'active_support'
3
+
4
+ module ButtonToForm
5
+ class Engine < ::Rails::Engine
6
+ ActiveSupport.on_load(:action_controller) do
7
+ include ButtonToForm::ControllerMethods
8
+ end
9
+ end
10
+
11
+ module ControllerMethods
12
+ extend ActiveSupport::Concern
13
+ # TODO: Raise error if content_for :footer was never called in layout. But how do we even detect
14
+ # that?
15
+ =begin disabled
16
+ included do
17
+ after_action \
18
+ def ensure_button_to_form_rendered
19
+ puts %(@button_to_form_needs_to_render_layout_section=#{(@button_to_form_needs_to_render_layout_section).inspect})
20
+ puts %(=>@button_to_form_rendered_layout_section=#{(@button_to_form_rendered_layout_section).inspect})
21
+ if @button_to_form_needs_to_render_layout_section && !@button_to_form_rendered_layout_section
22
+ raise "button_to_form requires that you include content_for(:footer) somewhere in your layout"
23
+ end
24
+ end
25
+ end
26
+ =end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module ButtonToForm
2
+ Version = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: button_to_form
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tyler Rick
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ description: A button_to helper that can be used inside a form tag (unlike Rails's
42
+ own button_to)
43
+ email:
44
+ - tyler@tylerrick.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - ".travis.yml"
52
+ - Changelog.md
53
+ - Gemfile
54
+ - License
55
+ - Rakefile
56
+ - Readme.md
57
+ - app/helpers/button_to_form_helper.rb
58
+ - bin/console
59
+ - bin/setup
60
+ - button_to_form.gemspec
61
+ - config.ru
62
+ - lib/button_to_form.rb
63
+ - lib/button_to_form/engine.rb
64
+ - lib/button_to_form/version.rb
65
+ homepage: https://github.com/TylerRick/button_to_form
66
+ licenses: []
67
+ metadata:
68
+ homepage_uri: https://github.com/TylerRick/button_to_form
69
+ source_code_uri: https://github.com/TylerRick/button_to_form
70
+ changelog_uri: https://github.com/TylerRick/button_to_form/blob/master/Changelog.md
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 2.3.0
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubygems_version: 3.0.3
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: A button_to helper that can be used inside a form tag (unlike Rails's own
90
+ button_to)
91
+ test_files: []