hstore_radio_buttons 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +20 -13
- data/lib/hstore_radio_buttons.rb +2 -2
- data/lib/hstore_radio_buttons/button_definition.rb +13 -0
- data/lib/hstore_radio_buttons/configuration.rb +16 -9
- data/lib/hstore_radio_buttons/version.rb +1 -1
- data/test/lib/hstore_radio_buttons/button_definition_test.rb +31 -0
- data/test/lib/hstore_radio_buttons/configuration_test.rb +14 -14
- data/test/lib/hstore_radio_buttons/hstore_radio_buttons_test.rb +4 -0
- data/test/support/models.rb +7 -0
- metadata +5 -5
- data/lib/hstore_radio_buttons/button_options.rb +0 -9
- data/test/lib/hstore_radio_buttons/button_options_test.rb +0 -15
data/README.md
CHANGED
@@ -97,7 +97,7 @@ within the model itself.
|
|
97
97
|
class Report < ActiveRecord::Base
|
98
98
|
include HstoreRadioButtons
|
99
99
|
|
100
|
-
hstore_radio_button Hash[
|
100
|
+
hstore_radio_button Hash[viewed: ['true', 'false']]
|
101
101
|
hstore_radio_button Hash['written by' => %w(monkeys interns milton)]
|
102
102
|
end
|
103
103
|
|
@@ -235,33 +235,39 @@ And then do a render like:
|
|
235
235
|
|
236
236
|
<%= f.label(:gender) %>
|
237
237
|
or
|
238
|
+
|
238
239
|
<%= f.hstore_radio_button(:gender) %>
|
239
240
|
|
240
241
|
Then your button set will start with the label "Gender". But what if you
|
241
242
|
want something else? Use the [Rails translations api](http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models). So in your en.yml file, you could put:
|
242
243
|
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
244
|
+
en:
|
245
|
+
activerecord:
|
246
|
+
attributes:
|
247
|
+
person:
|
248
|
+
gender: "Please pick a gender"
|
248
249
|
|
249
250
|
And your radio button set will render as:
|
250
251
|
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
252
|
+
Please pick a gender
|
253
|
+
o Male
|
254
|
+
o Female
|
255
|
+
o Other
|
255
256
|
|
256
|
-
|
257
|
+
## Persistence and Associations
|
257
258
|
|
258
259
|
The conversion of your data into an hstore is handled by
|
259
260
|
[activerecord-postgres-store]
|
260
261
|
(https://github.com/engageis/activerecord-postgres-hstore) so refer to
|
261
262
|
their documentation.
|
262
263
|
|
263
|
-
|
264
|
-
|
264
|
+
Any model that includes HstoreRadioButtons will have a has_one
|
265
|
+
associaton with `hstore_radio_data`. This relationship will have
|
266
|
+
`:autosave => true`
|
267
|
+
|
268
|
+
As you might guess from that association, your data will be stored in
|
269
|
+
the hstore_radio_data table. If you saved data for a Person with the
|
270
|
+
id of 1, it will be saved as
|
265
271
|
|
266
272
|
model_id model_type hstore_data
|
267
273
|
1 Person {'gender' => 'other'....}
|
@@ -284,6 +290,7 @@ contributed by [Davin Lagerroos](https://github.com/davinlagerroos).
|
|
284
290
|
|
285
291
|
### Versions
|
286
292
|
|
293
|
+
- 0.0.4 - Fixes issues: 1, 2
|
287
294
|
- 0.0.3 - Added support to customize button-set labels with Rails Internationalization API
|
288
295
|
- 0.0.2 - Fixes to inconsistency in how you call buttons by name.
|
289
296
|
- 0.0.1 - Initial
|
data/lib/hstore_radio_buttons.rb
CHANGED
@@ -4,7 +4,7 @@ require 'active_support/dependencies/autoload'
|
|
4
4
|
|
5
5
|
require 'hstore_radio_buttons/version'
|
6
6
|
require 'hstore_radio_buttons/configuration'
|
7
|
-
require 'hstore_radio_buttons/
|
7
|
+
require 'hstore_radio_buttons/button_definition'
|
8
8
|
require 'hstore_radio_buttons/button_set'
|
9
9
|
require 'hstore_radio_buttons/method_namer'
|
10
10
|
require 'hstore_radio_buttons/helpers/form_helper'
|
@@ -35,6 +35,6 @@ module HstoreRadioButtons
|
|
35
35
|
private :hstore_data_proxy
|
36
36
|
|
37
37
|
included do
|
38
|
-
has_one :hstore_radio_data, :class_name => 'HstoreRadioButtons::HstoreRadioData', :as => :model
|
38
|
+
has_one :hstore_radio_data, :class_name => 'HstoreRadioButtons::HstoreRadioData', :as => :model, :autosave => :true
|
39
39
|
end
|
40
40
|
end
|
@@ -13,14 +13,14 @@ module HstoreRadioButtons
|
|
13
13
|
self.model = model
|
14
14
|
|
15
15
|
if yaml_exists?
|
16
|
-
|
16
|
+
button_defititions_from_yaml.each {|button_definition| build_button_from_definition(button_definition, model)}
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
def _from_hash(model, raw_button_options)
|
21
21
|
self.model = model
|
22
|
-
|
23
|
-
|
22
|
+
button_definition = create_button_definiton(raw_button_options)
|
23
|
+
build_button_from_definition(button_definition,model)
|
24
24
|
end
|
25
25
|
|
26
26
|
private
|
@@ -33,16 +33,23 @@ module HstoreRadioButtons
|
|
33
33
|
File.exists?(yaml_file_location)
|
34
34
|
end
|
35
35
|
|
36
|
-
def
|
37
|
-
|
36
|
+
def button_defititions_from_yaml
|
37
|
+
button_definitions = []
|
38
38
|
YAML.load(config_file)[model.to_s.downcase].each do |key, value|
|
39
|
-
|
39
|
+
button_definitions << create_button_definiton(Hash[key,value])
|
40
40
|
end
|
41
|
-
|
41
|
+
button_definitions
|
42
42
|
end
|
43
43
|
|
44
|
-
def
|
45
|
-
|
44
|
+
def create_button_definiton(raw_button_options)
|
45
|
+
b = ButtonDefinition.new
|
46
|
+
b.name = raw_button_options.keys.first
|
47
|
+
b.options = raw_button_options.values.first
|
48
|
+
b
|
49
|
+
end
|
50
|
+
|
51
|
+
def build_button_from_definition(button_definition,model)
|
52
|
+
HstoreRadioButtons::ButtonSet.new(button_definition,model)
|
46
53
|
end
|
47
54
|
end
|
48
55
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative '../../test_helper.rb'
|
2
|
+
|
3
|
+
describe HstoreRadioButtons::ButtonDefinition do
|
4
|
+
before :each do
|
5
|
+
@it = HstoreRadioButtons::ButtonDefinition.new
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "#name" do
|
9
|
+
it "returns a string when set to a string" do
|
10
|
+
@it.name = "string"
|
11
|
+
@it.name.must_equal "string"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns a string when set to a symbol" do
|
15
|
+
@it.name = :symbol
|
16
|
+
@it.name.must_equal "symbol"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#options" do
|
21
|
+
it "turns a single option into an array" do
|
22
|
+
@it.options = 1
|
23
|
+
@it.options.must_equal [1]
|
24
|
+
end
|
25
|
+
|
26
|
+
it "turns a null options into an array" do
|
27
|
+
@it.options = nil
|
28
|
+
@it.options.must_equal []
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -23,13 +23,13 @@ describe HstoreRadioButtons::Configuration do
|
|
23
23
|
@it = Person.new
|
24
24
|
config = YAML.load(File.open(@test_yaml_file_location))
|
25
25
|
@button_sets = config[@it.class.to_s.downcase]
|
26
|
-
@
|
27
|
-
HstoreRadioButtons::
|
26
|
+
@button_definition = button_definition_double('test',[])
|
27
|
+
HstoreRadioButtons::ButtonDefinition.stubs(:new).returns(@button_definition)
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'creates a button_set for each button set defined in the config file' do
|
31
31
|
@button_sets.each do |button_set|
|
32
|
-
HstoreRadioButtons::ButtonSet.expects(:new).with(@
|
32
|
+
HstoreRadioButtons::ButtonSet.expects(:new).with(@button_definition,@it.class).returns(nil)
|
33
33
|
end
|
34
34
|
HstoreRadioButtons::Configuration.from_yaml(Person, @test_yaml_file_location)
|
35
35
|
end
|
@@ -39,19 +39,19 @@ describe HstoreRadioButtons::Configuration do
|
|
39
39
|
describe "#from_hash" do
|
40
40
|
describe "accepts a model and a button-defining hash to create a button" do
|
41
41
|
it "creates a button_set for each button defined by a hstore_radio_button macro" do
|
42
|
-
|
43
|
-
|
44
|
-
HstoreRadioButtons::
|
42
|
+
button_definition = button_definition_double(:viewed, ['true','false'])
|
43
|
+
HstoreRadioButtons::ButtonDefinition.expects(:new).returns(button_definition)
|
44
|
+
HstoreRadioButtons::ButtonSet.expects(:new).with(button_definition,Report)
|
45
45
|
|
46
|
-
HstoreRadioButtons::
|
47
|
-
|
48
|
-
class Report < ActiveRecord::Base
|
49
|
-
include HstoreRadioButtons
|
50
|
-
|
51
|
-
hstore_radio_button Hash['viewed' => ['true', 'false']]
|
52
|
-
hstore_radio_button Hash['written by' => %w(monkeys interns milton)]
|
53
|
-
end
|
46
|
+
HstoreRadioButtons::Configuration.from_hash(Report, Hash[viewed: ['true','false']])
|
54
47
|
end
|
55
48
|
end
|
56
49
|
end
|
57
50
|
end
|
51
|
+
|
52
|
+
def button_definition_double(name, options)
|
53
|
+
b = HstoreRadioButtons::ButtonDefinition.new
|
54
|
+
b.name = name
|
55
|
+
b.options = options
|
56
|
+
b
|
57
|
+
end
|
@@ -13,4 +13,8 @@ describe HstoreRadioButtons do
|
|
13
13
|
Person.new.must_respond_to :hstore_radio_data
|
14
14
|
Person.new.must_respond_to :build_hstore_radio_data
|
15
15
|
end
|
16
|
+
|
17
|
+
it "sets autosave to true for the hstore_radio_data table" do
|
18
|
+
Person.reflect_on_association(:hstore_radio_data).options[:autosave].must_equal :true #yes, it is a symbol
|
19
|
+
end
|
16
20
|
end
|
data/test/support/models.rb
CHANGED
@@ -4,3 +4,10 @@ class Person < ActiveRecord::Base
|
|
4
4
|
hstore_radio_buttons './test/support/config/hstore_radio_button_sets.yml'
|
5
5
|
end
|
6
6
|
|
7
|
+
class Report < ActiveRecord::Base
|
8
|
+
include HstoreRadioButtons
|
9
|
+
|
10
|
+
hstore_radio_button Hash[viewed: ['true', 'false']]
|
11
|
+
hstore_radio_button Hash['written by' => %w(monkeys interns milton)]
|
12
|
+
end
|
13
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hstore_radio_buttons
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-06-
|
12
|
+
date: 2013-06-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -157,14 +157,14 @@ files:
|
|
157
157
|
- lib/generators/hstore_radio_buttons/migration/migration_generator.rb
|
158
158
|
- lib/generators/hstore_radio_buttons/migration/templates/migration.rb
|
159
159
|
- lib/hstore_radio_buttons.rb
|
160
|
-
- lib/hstore_radio_buttons/
|
160
|
+
- lib/hstore_radio_buttons/button_definition.rb
|
161
161
|
- lib/hstore_radio_buttons/button_set.rb
|
162
162
|
- lib/hstore_radio_buttons/configuration.rb
|
163
163
|
- lib/hstore_radio_buttons/helpers/form_helper.rb
|
164
164
|
- lib/hstore_radio_buttons/hstore_radio_data.rb
|
165
165
|
- lib/hstore_radio_buttons/method_namer.rb
|
166
166
|
- lib/hstore_radio_buttons/version.rb
|
167
|
-
- test/lib/hstore_radio_buttons/
|
167
|
+
- test/lib/hstore_radio_buttons/button_definition_test.rb
|
168
168
|
- test/lib/hstore_radio_buttons/button_set_test.rb
|
169
169
|
- test/lib/hstore_radio_buttons/configuration_test.rb
|
170
170
|
- test/lib/hstore_radio_buttons/helpers/form_builder_test.rb
|
@@ -203,7 +203,7 @@ summary: Define a set of radio buttons for a model and then save the perisisted
|
|
203
203
|
for these buttons in an hstore field. Good for data that you want to persist, but
|
204
204
|
that isn't important enough for its own field in the model.
|
205
205
|
test_files:
|
206
|
-
- test/lib/hstore_radio_buttons/
|
206
|
+
- test/lib/hstore_radio_buttons/button_definition_test.rb
|
207
207
|
- test/lib/hstore_radio_buttons/button_set_test.rb
|
208
208
|
- test/lib/hstore_radio_buttons/configuration_test.rb
|
209
209
|
- test/lib/hstore_radio_buttons/helpers/form_builder_test.rb
|
@@ -1,15 +0,0 @@
|
|
1
|
-
require_relative '../../test_helper.rb'
|
2
|
-
|
3
|
-
describe HstoreRadioButtons::ButtonOptions do
|
4
|
-
describe "#options" do
|
5
|
-
it "turns a single option into an array" do
|
6
|
-
it = HstoreRadioButtons::ButtonOptions.new('test', 1)
|
7
|
-
it.options.must_equal [1]
|
8
|
-
end
|
9
|
-
|
10
|
-
it "turns a null options into an array" do
|
11
|
-
it = HstoreRadioButtons::ButtonOptions.new('test', nil)
|
12
|
-
it.options.must_equal []
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|