form-select 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +72 -0
- data/Rakefile +34 -0
- data/app/controllers/form-select/application_controller.rb +7 -0
- data/config/routes.rb +4 -0
- data/lib/form-select.rb +10 -0
- data/lib/form_select/engine.rb +7 -0
- data/lib/form_select/model.rb +42 -0
- data/lib/form_select/version.rb +5 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3ba4b029d1d9779c6597c16dd36e254257ddf7b8
|
4
|
+
data.tar.gz: fa383e0f8bbc4d16b3c3d934bae53e15ec487f20
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 66afdfc3af49a62427b7821d1703a88d1c7d5e1b5c5e4f4348667f8c35f5bfa6c466c7f6a96db8c6d338e97637e2cb25c89df3de26a18c26d66db069f288e5d9
|
7
|
+
data.tar.gz: 6e4ce84c00f66e290a81c3419f29db40c9f8a457bbb9ee9f37718d9ac051a25df37231c066386aac0a0be7bfef89998d05f402b1937e41a34987b8d0641df26a
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 Jason Lee
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# FormSelect
|
2
|
+
|
3
|
+
A simple improve for Rails form select helper.
|
4
|
+
|
5
|
+
We need use `form.select ::category_id, Category.collect { |record| [record.name, record.id] }` in so many case.
|
6
|
+
|
7
|
+
This gem give use a simple way.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
```rb
|
12
|
+
gem "form-select"
|
13
|
+
```
|
14
|
+
|
15
|
+
app/model/category.rb
|
16
|
+
|
17
|
+
```rb
|
18
|
+
class Category
|
19
|
+
form_select :name, scope: -> { order("name asc") }
|
20
|
+
form_select :reverse_name, text_method: :name, scope: -> { order("name desc") }
|
21
|
+
end
|
22
|
+
|
23
|
+
class User
|
24
|
+
form_select :login, value_method: :to_param, scope: -> { order("id desc") }
|
25
|
+
form_select :email
|
26
|
+
|
27
|
+
def to_param
|
28
|
+
login.downcase
|
29
|
+
end
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
Now you got the helper methods:
|
34
|
+
|
35
|
+
```rb
|
36
|
+
irb> Category.name_options
|
37
|
+
=> [["Android", 3], ["iOS", 2], ["News", 1]]
|
38
|
+
irb> Category.reverse_name_options
|
39
|
+
=> [["News", 1], ["iOS", 2], ["Android", 3]]
|
40
|
+
irb> User.login_options
|
41
|
+
=> [["Foo", "foo"], ["Mike", "mike"], ["Jason", "jason"]]
|
42
|
+
irb> User.email_options
|
43
|
+
=> [["jason@gmail.com", 10], ["mike@foo.com", 11], ["foo@bar.com", 12]]
|
44
|
+
```
|
45
|
+
|
46
|
+
So you can easy use it in Rails form:
|
47
|
+
|
48
|
+
```erb
|
49
|
+
<%= form_with(model: post, local: true) do |form| %>
|
50
|
+
<div class="field">
|
51
|
+
<%= form.label :title %>
|
52
|
+
<%= form.text_field :title %>
|
53
|
+
</div>
|
54
|
+
|
55
|
+
<div class="field">
|
56
|
+
<%= form.label :user_id %>
|
57
|
+
<%= form.select :user_id, User.email_options %>
|
58
|
+
</div>
|
59
|
+
|
60
|
+
<div class="field">
|
61
|
+
<%= form.label :category_id %>
|
62
|
+
<%= form.select :category_id, Category.name_options %>
|
63
|
+
</div>
|
64
|
+
|
65
|
+
<div class="actions">
|
66
|
+
<%= form.submit %>
|
67
|
+
</div>
|
68
|
+
<% end %>
|
69
|
+
```
|
70
|
+
|
71
|
+
## License
|
72
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
begin
|
4
|
+
require "bundler/setup"
|
5
|
+
rescue LoadError
|
6
|
+
puts "You must `gem install bundler` and `bundle install` to run rake tasks"
|
7
|
+
end
|
8
|
+
|
9
|
+
require "rdoc/task"
|
10
|
+
|
11
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
12
|
+
rdoc.rdoc_dir = "rdoc"
|
13
|
+
rdoc.title = "Form::Select"
|
14
|
+
rdoc.options << "--line-numbers"
|
15
|
+
rdoc.rdoc_files.include("README.md")
|
16
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
17
|
+
end
|
18
|
+
|
19
|
+
APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
|
20
|
+
load "rails/tasks/engine.rake"
|
21
|
+
|
22
|
+
load "rails/tasks/statistics.rake"
|
23
|
+
|
24
|
+
require "bundler/gem_tasks"
|
25
|
+
|
26
|
+
require "rake/testtask"
|
27
|
+
|
28
|
+
Rake::TestTask.new(:test) do |t|
|
29
|
+
t.libs << "test"
|
30
|
+
t.pattern = "test/**/*_test.rb"
|
31
|
+
t.verbose = false
|
32
|
+
end
|
33
|
+
|
34
|
+
task default: :test
|
data/config/routes.rb
ADDED
data/lib/form-select.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FormSelect
|
4
|
+
module Model
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
# Use for define helper methods for Rails form.select tag
|
12
|
+
#
|
13
|
+
# for example:
|
14
|
+
#
|
15
|
+
# class User < ApplicationRecord
|
16
|
+
# form_select :name, scope: -> { order("name asc") }
|
17
|
+
# form_select :email, text_method: :name, value_method: :email, scope: -> { where(status: :active).order("id desc") }
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# <div class="field">
|
21
|
+
# <%= form.label :user_id %>
|
22
|
+
# <%= form.select :user_id, User.name_options %>
|
23
|
+
# </div>
|
24
|
+
# <div class="field">
|
25
|
+
# <%= form.label :email %>
|
26
|
+
# <%= form.select :email, User.email_options %>
|
27
|
+
# </div>
|
28
|
+
#
|
29
|
+
def form_select(method, text_method: nil, value_method: nil, scope: nil)
|
30
|
+
method_name = "#{method}_options"
|
31
|
+
|
32
|
+
text_method ||= method
|
33
|
+
value_method ||= primary_key
|
34
|
+
scope ||= -> { all }
|
35
|
+
|
36
|
+
define_singleton_method(method_name) do
|
37
|
+
scope.call.collect { |record| [record.send(text_method), record.send(value_method)] }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: form-select
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason Lee
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
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: mysql2
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Helper of form select options.
|
42
|
+
email:
|
43
|
+
- huacnlee@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- app/controllers/form-select/application_controller.rb
|
52
|
+
- config/routes.rb
|
53
|
+
- lib/form-select.rb
|
54
|
+
- lib/form_select/engine.rb
|
55
|
+
- lib/form_select/model.rb
|
56
|
+
- lib/form_select/version.rb
|
57
|
+
homepage: https://github.com/rails-engine/form-select
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.6.14
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: Helper of form select options
|
81
|
+
test_files: []
|