simple_form-magic_submit 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_form-magic_submit.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Andrey Subbotin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # Magic Submit Button for Simple Form
2
+
3
+ A smart submit button extension for the Simple Form gem that:
4
+
5
+ - Displays an alternate title on validation errors.
6
+ - Displays a different "Loading..." title depending on whether the model is being saved or updated.
7
+ - Is optionally postfixed with a "Cancel" link.
8
+ - Is fully localizable via the I18n gem.
9
+
10
+ ## A picture's worth a thousand words, right?
11
+
12
+ For a newly created model object, typically in a `new.html.haml`:
13
+
14
+ <img src="https://raw.github.com/eploko/simple_form-magic_submit/master/preview/01-submit.png" width="720" height="190" alt="A preview of the submit button in the simples case."/>
15
+
16
+ Add a cancel link:
17
+
18
+ <img src="https://raw.github.com/eploko/simple_form-magic_submit/master/preview/02-submit-with-cancel.png" width="720" height="190" alt="A preview of the submit button accompanied by a Cancel link."/>
19
+
20
+ When something goes wrong and the model fails validation:
21
+
22
+ <img src="https://raw.github.com/eploko/simple_form-magic_submit/master/preview/03-model-invalid.png" width="720" height="260" alt="A preview of the submit button with a different title when the validation has failed."/>
23
+
24
+ And all this and a bit more with just a single line of code:
25
+
26
+ ```haml
27
+ = f.button :submit_retry, cancel: root_path
28
+ ```
29
+
30
+ ## Installation
31
+
32
+ Add this line to your application's Gemfile:
33
+
34
+ gem 'simple_form-magic_submit'
35
+
36
+ And then execute:
37
+
38
+ $ bundle
39
+
40
+ Or install it yourself as:
41
+
42
+ $ gem install simple_form-magic_submit
43
+
44
+ ## Usage
45
+
46
+ In your view template add it allong the lines of:
47
+
48
+ ```haml
49
+ = simple_form_for(resource) do |f|
50
+ = f.error_notification
51
+ .form-inputs
52
+ = f.input :email, autofocus: true
53
+ .form-actions
54
+ = f.button :submit_retry
55
+ ```
56
+
57
+ The last one is this gem's magic.
58
+
59
+ To add a cancel link:
60
+
61
+ ```haml
62
+ = simple_form_for(resource) do |f|
63
+ = f.error_notification
64
+ .form-inputs
65
+ = f.input :email, autofocus: true
66
+ .form-actions
67
+ = f.button :submit_retry, cancel: root_path
68
+ ```
69
+
70
+ Replace `root_path` with whatever path is relevant for your app.
71
+
72
+ ## Localization
73
+
74
+ The gem comes bundled with an English translation in [en.yml](https://github.com/eploko/simple_form-magic_submit/blob/master/locales/en.yml):
75
+
76
+ ```yaml
77
+ en:
78
+ simple_form:
79
+ magic_submit:
80
+ default:
81
+ create:
82
+ submit: "Create a new %{model}"
83
+ retry: "Try creating once again"
84
+ loading: "Creating&#133;"
85
+ save:
86
+ submit: "Save changes"
87
+ retry: "Try saving once again"
88
+ loading: "Saving&#133;"
89
+ cancel:
90
+ format: "%{submit_button} or %{cancel_link}"
91
+ cancel: "Cancel"
92
+ # Replace 'model_name' below with the name of your model and
93
+ # alter the transaltions as you wish.
94
+ #
95
+ # Each translation will have %{model} interpolated.
96
+ #
97
+ # model_name:
98
+ # create:
99
+ # submit: "Create a new %{model}"
100
+ # retry: "Try creating once again"
101
+ # loading: "Creating&#133;"
102
+ # save:
103
+ # submit: "Save changes"
104
+ # retry: "Try saving once again"
105
+ # loading: "Saving&#133;"
106
+ ```
107
+
108
+ Simply copy the file to you config/locales folder inside your Rails project if you want to change the default strings.
109
+
110
+ ## To-do
111
+
112
+ 1. Add tests.
113
+ 2. Add more translations.
114
+ 3. You tell me.
115
+
116
+ ## Contributing
117
+
118
+ 1. Fork it
119
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
120
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
121
+ 4. Push to the branch (`git push origin my-new-feature`)
122
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ module SimpleForm
2
+ module MagicSubmit
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,41 @@
1
+ require "simple_form/magic_submit/version"
2
+
3
+ module SimpleForm
4
+ module MagicSubmit
5
+
6
+ def submit_retry_button(*args, &block)
7
+ options = args.extract_options!
8
+ options[:"data-loading-text"] ||= translate_key(:loading)
9
+ options[:class] = ['btn-primary', 'btn-submit', options[:class]].compact
10
+ options[:id] ||= "submit_#{object_scope}"
11
+ args << options
12
+ if cancel = options.delete(:cancel)
13
+ I18n.t("simple_form.magic_submit.cancel.format",
14
+ submit_button: submit(translate_key, *args, &block).html_safe,
15
+ cancel_link: template.link_to(I18n.t('simple_form.magic_submit.cancel.cancel').html_safe, cancel)
16
+ )
17
+ else
18
+ submit(translate_key, *args, &block)
19
+ end.html_safe
20
+ end
21
+
22
+ private
23
+
24
+ def object_scope
25
+ self.object.class.model_name.underscore
26
+ end
27
+
28
+ def translate_key(key = nil)
29
+ op = self.object.new_record? ? :create : :save
30
+ key ||= self.object.errors.count > 0 ? :retry : :submit
31
+ I18n.t("simple_form.magic_submit.#{object_scope}.#{op}.#{key}",
32
+ default: [:"simple_form.magic_submit.default.#{op}.#{key}", :"helpers.submit.#{op}"],
33
+ model: self.object.class.model_name.titlecase
34
+ ).html_safe
35
+ end
36
+
37
+ end
38
+ end
39
+
40
+ SimpleForm::FormBuilder.send :include, SimpleForm::MagicSubmit
41
+ I18n.load_path += Dir.glob(File.join(File.dirname(__FILE__), '..', '..', 'locales', '*.{rb,yml}'))
data/locales/en.yml ADDED
@@ -0,0 +1,29 @@
1
+ en:
2
+ simple_form:
3
+ magic_submit:
4
+ default:
5
+ create:
6
+ submit: "Create a new %{model}"
7
+ retry: "Try creating once again"
8
+ loading: "Creating&#133;"
9
+ save:
10
+ submit: "Save changes"
11
+ retry: "Try saving once again"
12
+ loading: "Saving&#133;"
13
+ cancel:
14
+ format: "%{submit_button} or %{cancel_link}"
15
+ cancel: "Cancel"
16
+ # Replace 'model_name' below with the name of your model and
17
+ # alter the transaltions as you wish.
18
+ #
19
+ # Each translation will have %{model} interpolated.
20
+ #
21
+ # model_name:
22
+ # create:
23
+ # submit: "Create a new %{model}"
24
+ # retry: "Try creating once again"
25
+ # loading: "Creating&#133;"
26
+ # save:
27
+ # submit: "Save changes"
28
+ # retry: "Try saving once again"
29
+ # loading: "Saving&#133;"
Binary file
Binary file
Binary file
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_form/magic_submit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_form-magic_submit"
8
+ spec.version = SimpleForm::MagicSubmit::VERSION
9
+ spec.authors = ["Andrey Subbotin"]
10
+ spec.email = ["andrey@subbotin.me"]
11
+ spec.description = "A smart submit button extension for the Simple Form gem."
12
+ spec.summary = "Magic Submit Button for Simple Form"
13
+ spec.homepage = 'http://rubygems.org/gems/simple_form-magic_submit'
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "simple_form", "~> 2.0"
22
+ spec.add_runtime_dependency "i18n", "~> 0.6"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec", "~> 2.5"
27
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_form-magic_submit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrey Subbotin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: simple_form
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: i18n
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.6'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.6'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '2.5'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '2.5'
94
+ description: A smart submit button extension for the Simple Form gem.
95
+ email:
96
+ - andrey@subbotin.me
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - lib/simple_form/magic_submit.rb
107
+ - lib/simple_form/magic_submit/version.rb
108
+ - locales/en.yml
109
+ - preview/01-submit.png
110
+ - preview/02-submit-with-cancel.png
111
+ - preview/03-model-invalid.png
112
+ - simple_form-magic_submit.gemspec
113
+ homepage: http://rubygems.org/gems/simple_form-magic_submit
114
+ licenses:
115
+ - MIT
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 1.8.25
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Magic Submit Button for Simple Form
138
+ test_files: []