enum_render 0.1.0 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d079c03a997d76b80e086545292e45a8417eec27b9753c44b6180c1ae4e4d552
4
- data.tar.gz: 9d853a07928fd087a3d30e7a9839b35e165b7e10bef600c6b59407fc61a1105e
3
+ metadata.gz: 8a9e85d6ac41aae634ac733821f6df934f15549290a02df5891321ed4ebad2bd
4
+ data.tar.gz: ff2c827b132fb5bbaaee8b8c654ca5d7baa06f0305d7f16e86ae41f18242060b
5
5
  SHA512:
6
- metadata.gz: 4e4dc80d62fe8315c81bf9baa1f74cf8a271df4c3b8c9737aa1f8419522bb42513b1815ed1db17c0334826b056c06c0dc65fb1d09673f46e23d0e1231a033c4c
7
- data.tar.gz: 92cff7b6f8525480f23191afa980d08043c7c68b555d100e71e4c0b88785b43d85967480ec506b52a72787648514090aa5363e3e55a971d7d24e46a9e346026b
6
+ metadata.gz: c5934a60999dc8e3a06f999a7dbfac3c36425f3876a5a2c1b47b439ac8be8be4a7688dfcf8011cc6ef6c4875a9f31adc7793c4a2901a5b6bf974e07a2bf6e00b
7
+ data.tar.gz: bf545f3d64e595d11b328119ca42515a4ba3f95fea5dbbb57e929922c71c9d6ea9e34018f0318816135a6044529a3207412ee4363f76da82ef1a54e04064a73d
data/README.md CHANGED
@@ -1,9 +1,8 @@
1
+ Inspired by the [template](https://github.com/huacnlee/enumize)
2
+
1
3
  # EnumRender
2
4
  Make enum translate friendly
3
5
 
4
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/enum_render`. To experiment with that code, run `bin/console` for an interactive prompt.
5
-
6
- TODO: Delete this and the text above, and describe your gem
7
6
 
8
7
  ## Installation
9
8
 
@@ -17,29 +16,67 @@ And then execute:
17
16
 
18
17
  $ bundle install
19
18
 
20
- Or install it yourself as:
21
-
22
- $ gem install enum_render
23
-
24
19
  ## Usage
25
20
 
26
- TODO: Write usage instructions here
21
+ ```ruby
22
+ # app/model/event.rb
23
+ class Event
24
+ enum status: {pending: 0, approved: 1, declined: 2}
25
+ end
26
+ ```
27
27
 
28
- ## Development
28
+ In I18n translate file:
29
+ ```yaml
30
+ # config/locales/en.yml
31
+ en:
32
+ enums:
33
+ event:
34
+ status:
35
+ pending: "Pending"
36
+ approved: "Approved"
37
+ declined: "Declined"
38
+
39
+ # config/locales/ja.yml
40
+ ja:
41
+ enums:
42
+ event:
43
+ status:
44
+ pending: "保留中"
45
+ approved: "承認済み"
46
+ declined: "辞退"
47
+ ```
48
+ We have methods: `Event.statuses` and `Event.first.status`
29
49
 
30
- 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.
50
+ After install we have methods:
31
51
 
32
- 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).
52
+ ```ruby
53
+ [2] pry(main)> Event.status_options
54
+ [{"key"=>"pending", "value"=>"保留中"}, {"key"=>"approved", "value"=>"承認済み"}, {"key"=>"declined", "value"=>"辞退"}]
55
+ ```
33
56
 
34
- ## Contributing
57
+ ```ruby
58
+ [6] pry(main)> Event.first.status_option
59
+ {"key"=>"approved", "value"=>"承認済み"}
60
+ ```
35
61
 
36
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/enum_render. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/enum_render/blob/master/CODE_OF_CONDUCT.md).
62
+ ```ruby
63
+ [9] pry(main)> Event.first.status_name
64
+ => "承認済み"
65
+ ```
37
66
 
67
+ ```ruby
68
+ [8] pry(main)> Event.first.status_value
69
+ => 1
70
+ ```
38
71
 
72
+ ```ruby
73
+ [10] pry(main)> Event.status_select
74
+ => [["保 留 中 ", "pending"], ["承 認 済 み ", "approved"], ["辞 退 ", "declined"]]
75
+ ```
39
76
  ## License
40
77
 
41
78
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
42
79
 
43
80
  ## Code of Conduct
44
81
 
45
- Everyone interacting in the EnumRender project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/enum_render/blob/master/CODE_OF_CONDUCT.md).
82
+ Everyone interacting in the EnumRender project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sangvo/enum_render/blob/master/CODE_OF_CONDUCT.md).
Binary file
data/lib/enum_render.rb CHANGED
@@ -35,11 +35,10 @@ module EnumRender
35
35
  define_method("#{name}_option") do
36
36
  return nil if self.send(name).nil?
37
37
 
38
- value = klass.send(name.to_s.pluralize)[self[name]]
39
- label = klass.new(name => value).send("#{name}_name")
38
+ label = klass.new(name => self[name]).send("#{name}_name")
40
39
  {
41
- "id" => value,
42
- "label" => label
40
+ "key" => self[name],
41
+ "value" => label
43
42
  }
44
43
  end
45
44
 
@@ -47,13 +46,22 @@ module EnumRender
47
46
  define_singleton_method("#{name}_options") do
48
47
  self.send(name.to_s.pluralize).map do |k, _|
49
48
  label = self.new(name => k).send("#{name}_name")
50
- value = self.new(name => k).send("#{name}_value")
49
+ key = self.new(name => k).send("#{name}")
51
50
  {
52
- "id" => value,
53
- "label" => label
51
+ "key" => key,
52
+ "value" => label
54
53
  }
55
54
  end
56
55
  end
56
+
57
+ detect_enum_conflict!(name, "#{name}_select", true)
58
+ define_singleton_method("#{name}_select") do
59
+ self.send(name.to_s.pluralize).map do |k, _|
60
+ label = self.new(name => k).send("#{name}_name")
61
+ key = self.new(name => k).send("#{name}")
62
+ [label, key]
63
+ end
64
+ end
57
65
  end
58
66
  end
59
67
  end
@@ -1,3 +1,3 @@
1
1
  module EnumRender
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enum_render
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - sangvo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-25 00:00:00.000000000 Z
11
+ date: 2021-03-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Make enum dropdown friendly
14
14
  email:
@@ -27,6 +27,7 @@ files:
27
27
  - Rakefile
28
28
  - bin/console
29
29
  - bin/setup
30
+ - enum_render-0.1.0.gem
30
31
  - enum_render.gemspec
31
32
  - lib/enum_render.rb
32
33
  - lib/enum_render/railtie.rb