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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ba4b029d1d9779c6597c16dd36e254257ddf7b8
4
- data.tar.gz: fa383e0f8bbc4d16b3c3d934bae53e15ec487f20
3
+ metadata.gz: 61a7e843bb09d1878d151673b4a408f84a2b9c95
4
+ data.tar.gz: fbe4ea1384d8b012e9f38a21d6be472f22a55e38
5
5
  SHA512:
6
- metadata.gz: 66afdfc3af49a62427b7821d1703a88d1c7d5e1b5c5e4f4348667f8c35f5bfa6c466c7f6a96db8c6d338e97637e2cb25c89df3de26a18c26d66db069f288e5d9
7
- data.tar.gz: 6e4ce84c00f66e290a81c3419f29db40c9f8a457bbb9ee9f37718d9ac051a25df37231c066386aac0a0be7bfef89998d05f402b1937e41a34987b8d0641df26a
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, text_method: :name, scope: -> { order("name desc") }
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, value_method: :to_param, scope: -> { order("id desc") }
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:
@@ -14,7 +14,8 @@ module FormSelect
14
14
  #
15
15
  # class User < ApplicationRecord
16
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") }
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, text_method: nil, value_method: nil, scope: nil)
30
+ def form_select(method, field: nil, scope: nil)
30
31
  method_name = "#{method}_options"
31
32
 
32
- text_method ||= method
33
- value_method ||= primary_key
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FormSelect
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: form-select
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
  - Jason Lee