rails-for_select 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rails/for_select.rb +25 -0
  3. metadata +86 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9582ba0724afa04eca33ee2b7db5e063c1f10810
4
+ data.tar.gz: ca91c3fee2077c4468d46f5e6e06e55cd950b6ab
5
+ SHA512:
6
+ metadata.gz: d9a9f8f670a0c957a96f5bebc1d591125652b3a25c8949ca937ffd0e8c7bb036c08f2deab5306fcb8931d85990c8e9c33f024c19f3b36df400d9352dfbecea3b
7
+ data.tar.gz: ef9b1ab2e0e7c2b94a72976f3bf96052bc83d40cc9fa89765701ac1313005debf3933b23cddce2c875e1997dc8985a7e62f53a997489abc405c0b1ff33db4a4e
@@ -0,0 +1,25 @@
1
+ module ForSelect
2
+ module ClassMethods
3
+ # Allow to call #for_select on each class inheriting from ActiveRecord::Base. Will generate a tuples array for use
4
+ # in Rails' form's select fields. It will cache the resulting tuples array.
5
+ #
6
+ # GameSystem.for_select # => [["name", 1], ["other name", 2], ...]
7
+ #
8
+ # For those models that doesn't defaultly have a name, or that should use another id field, you can customize it
9
+ # like this.
10
+ #
11
+ # GameSystem.for_select(id: :uuid) # => [["name", 'uuid1'], ["other name", 'uuid2'], ...]
12
+ # GameSystem.for_select(name: :full_name) # => [["full name", 1], ["other full name", 2], ...]
13
+ #
14
+ # If you're records doesn't have timestamps you can use another cache key.
15
+ #
16
+ # GameSystem.for_select(cache_key: :created_on) # => [["name", 1], ["other name", 2], ...]
17
+ def for_select(id: :id, name: :name, cache_key: :updated_at)
18
+ Rails.cache.fetch [self.to_s.tableize, :select, self.maximum(cache_key)] do
19
+ pluck(name, id)
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ ActiveRecord::Base.extend(ForSelect::ClassMethods)
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-for_select
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Emil Kampp
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-30 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: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ description: Exposes ActiveRecord::Base.for_select for use with Rails' ActionView::Helpers::FormTagHelper#select_tag
56
+ email: emil@kampp.me
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/rails/for_select.rb
62
+ homepage: https://github.com/ekampp/rails-for_select
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.4.5.1
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Exposes .for_select for use with Rails forms
86
+ test_files: []