form-select 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 +4 -4
- data/README.md +11 -2
- data/lib/form_select/model.rb +12 -4
- data/lib/form_select/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61a7e843bb09d1878d151673b4a408f84a2b9c95
|
4
|
+
data.tar.gz: fbe4ea1384d8b012e9f38a21d6be472f22a55e38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 195e4b77f0c78c1d25fd76e613170b8cc3a91c5b3e817c90c9119158bb1f0452fbc973f5e56e603d5c5c11a31bebab2a87d96a7d03a58828370faa2b8700556b
|
7
|
+
data.tar.gz: f09907dc90dddba6393d7fe69edb702def9ec3eab75f988cd38a54510017730d01d7d676f759bb022e8708ae90e818f01d76ac225ea273c6a9a7aa228dad43f8
|
data/README.md
CHANGED
@@ -17,17 +17,22 @@ app/model/category.rb
|
|
17
17
|
```rb
|
18
18
|
class Category
|
19
19
|
form_select :name, scope: -> { order("name asc") }
|
20
|
-
form_select :reverse_name,
|
20
|
+
form_select :reverse_name, field: [:name, :id], scope: -> { order("name desc") }
|
21
21
|
end
|
22
22
|
|
23
23
|
class User
|
24
|
-
form_select :login,
|
24
|
+
form_select :login, field: [:login, :to_param], scope: -> { order("id desc") }
|
25
25
|
form_select :email
|
26
|
+
form_select :email_value, field: [:email]
|
26
27
|
|
27
28
|
def to_param
|
28
29
|
login.downcase
|
29
30
|
end
|
30
31
|
end
|
32
|
+
|
33
|
+
class Post
|
34
|
+
form_select :author, field: [:author], scope: -> { where("author is not null").select(:author).distinct }
|
35
|
+
end
|
31
36
|
```
|
32
37
|
|
33
38
|
Now you got the helper methods:
|
@@ -41,6 +46,10 @@ irb> User.login_options
|
|
41
46
|
=> [["Foo", "foo"], ["Mike", "mike"], ["Jason", "jason"]]
|
42
47
|
irb> User.email_options
|
43
48
|
=> [["jason@gmail.com", 10], ["mike@foo.com", 11], ["foo@bar.com", 12]]
|
49
|
+
irb> User.email_value_options
|
50
|
+
=> [["jason@gmail.com", "jason@gmail.com"], ["mike@foo.com", "mike@foo.com"], ["foo@bar.com", "foo@bar.com"]]
|
51
|
+
irb> Post.author_options
|
52
|
+
=> [["Jason", "Jason"], ["Mike", "Mike"], ["Foor", "Foo"]]
|
44
53
|
```
|
45
54
|
|
46
55
|
So you can easy use it in Rails form:
|
data/lib/form_select/model.rb
CHANGED
@@ -14,7 +14,8 @@ module FormSelect
|
|
14
14
|
#
|
15
15
|
# class User < ApplicationRecord
|
16
16
|
# form_select :name, scope: -> { order("name asc") }
|
17
|
-
#
|
17
|
+
# form_select :name_value, field: [:name], scope: -> { order("name asc") }
|
18
|
+
# form_select :email, field: [:name, :email], scope: -> { where(status: :active).order("id desc") }
|
18
19
|
# end
|
19
20
|
#
|
20
21
|
# <div class="field">
|
@@ -26,11 +27,18 @@ module FormSelect
|
|
26
27
|
# <%= form.select :email, User.email_options %>
|
27
28
|
# </div>
|
28
29
|
#
|
29
|
-
def form_select(method,
|
30
|
+
def form_select(method, field: nil, scope: nil)
|
30
31
|
method_name = "#{method}_options"
|
31
32
|
|
32
|
-
|
33
|
-
|
33
|
+
if field.blank?
|
34
|
+
text_method ||= method
|
35
|
+
value_method ||= primary_key
|
36
|
+
else
|
37
|
+
field = [field] unless field.is_a?(Array)
|
38
|
+
text_method = field.first
|
39
|
+
value_method = field.last
|
40
|
+
end
|
41
|
+
|
34
42
|
scope ||= -> { all }
|
35
43
|
|
36
44
|
define_singleton_method(method_name) do
|
data/lib/form_select/version.rb
CHANGED