selections 0.1.7 → 0.1.11
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.
- data/CHANGELOG.md +4 -0
- data/README.md +16 -7
- data/lib/selections/form_builder_extensions.rb +42 -5
- data/lib/selections/version.rb +1 -1
- data/spec/selections/selections_tag_spec.rb +87 -30
- metadata +2 -2
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
# Selections
|
1
|
+
# Selections [](http://badge.fury.io/rb/selections)
|
2
2
|
|
3
3
|
Selection list management and form and view helpers.
|
4
4
|
|
5
5
|
##Key Features
|
6
6
|
|
7
|
-
* Manages one table to hold all selections items/
|
7
|
+
* Manages one table to hold all selections items/dropdown lists or Radio Buttons ( tree )
|
8
8
|
* Dynamic lookup to find parent or children ( eg. Selection.priorities )
|
9
|
-
* Form helper to display lists
|
9
|
+
* Form helper to display lists
|
10
|
+
- f.selections :priorities # dropdowns
|
11
|
+
- f.radios :priorities # radio buttons
|
10
12
|
* Model helpers for joining tables ( eg. belongs_to_selection :priority )
|
11
13
|
* Handling of archived items ( displaying if selected only )
|
12
14
|
* Ordering of lists based on alpha or numbered
|
@@ -69,9 +71,9 @@ Dynamic lookups support pluralization and will then return the children:
|
|
69
71
|
Selection.priorities -> [high,med,low] records
|
70
72
|
```
|
71
73
|
|
72
|
-
|
74
|
+
## Form Helper
|
73
75
|
|
74
|
-
|
76
|
+
If we had a controller for Ticket model with fields of:
|
75
77
|
|
76
78
|
* name
|
77
79
|
* priority_id
|
@@ -97,7 +99,7 @@ within the _form.html.erb just use the selections helper method:
|
|
97
99
|
<% end %>
|
98
100
|
```
|
99
101
|
|
100
|
-
###
|
102
|
+
### Selection List Options
|
101
103
|
|
102
104
|
```ruby
|
103
105
|
f.selections :fieldname, options = {}, html_options = {}
|
@@ -113,6 +115,14 @@ If you have a selection named differently to the foreign key eg. the foreign key
|
|
113
115
|
<%= f.selections :variety, :system_code => :category %>
|
114
116
|
```
|
115
117
|
|
118
|
+
### Radio Buttons Options
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
f.radios :ticket, options = {}
|
122
|
+
```
|
123
|
+
|
124
|
+
The radios method excepts all the standard Ruby on Rails form helper options and html formatting - http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-radio_button
|
125
|
+
|
116
126
|
### Scoped System Code
|
117
127
|
|
118
128
|
If you have naming conflicts/duplicates system_codes (parent codes) eg. category_id field for user and ticket models
|
@@ -196,7 +206,6 @@ Selections.model { YourSelectionModel }
|
|
196
206
|
|
197
207
|
* Add model generators
|
198
208
|
* Add selections management scaffold/generator
|
199
|
-
* Add Radio Button support
|
200
209
|
|
201
210
|
## Contributing
|
202
211
|
|
@@ -2,10 +2,9 @@ module Selections
|
|
2
2
|
module FormBuilderExtensions
|
3
3
|
# Create a select list based on the field name finding items within Selection.
|
4
4
|
#
|
5
|
-
#
|
6
5
|
# Example
|
7
6
|
# form_for(@ticket) do |f|
|
8
|
-
# f.
|
7
|
+
# f.select("priority")
|
9
8
|
#
|
10
9
|
# Uses priority_id from the ticket table and creates options list based on items in Selection table with a system_code of
|
11
10
|
# either priority or ticket_priority
|
@@ -16,7 +15,21 @@ module Selections
|
|
16
15
|
# * +system_code+ - Overrides the automatic system_code name based on the fieldname and looks up the list of items in Selection
|
17
16
|
|
18
17
|
def selections(field, options = {}, html_options = {})
|
19
|
-
SelectionTag.new(self, object, field, options, html_options).
|
18
|
+
SelectionTag.new(self, object, field, options, html_options).select_tag
|
19
|
+
end
|
20
|
+
|
21
|
+
# Build a radio button list based field name finding items within Selection
|
22
|
+
#
|
23
|
+
# Example
|
24
|
+
# form_for(@ticket) do |f|
|
25
|
+
# f.select :priority
|
26
|
+
# options
|
27
|
+
# * +system_code+ - Overrides the automatic system_code name based on the fieldname and looks up the list of items in Selection
|
28
|
+
|
29
|
+
def radios(field, options = {})
|
30
|
+
html_options = options.clone
|
31
|
+
html_options.delete_if {|key, value| key == :system_code}
|
32
|
+
SelectionTag.new(self, object, field, options, html_options).radio_tag
|
20
33
|
end
|
21
34
|
|
22
35
|
class SelectionTag #:nodoc:
|
@@ -51,7 +64,7 @@ module Selections
|
|
51
64
|
@items ||= system_code.children.filter_archived_except_selected(object.send(field_id))
|
52
65
|
end
|
53
66
|
|
54
|
-
def
|
67
|
+
def select_tag
|
55
68
|
if system_code
|
56
69
|
#TODO add default style
|
57
70
|
#html_options[:style] ||=
|
@@ -59,10 +72,30 @@ module Selections
|
|
59
72
|
options[:selected] = selected_item
|
60
73
|
form.select field_id, items.map { |item| [item.name, item.id] }, options, html_options
|
61
74
|
else
|
62
|
-
|
75
|
+
error_message
|
63
76
|
end
|
64
77
|
end
|
65
78
|
|
79
|
+
def radio_tag
|
80
|
+
if system_code
|
81
|
+
items.unshift(selection.new(name: blank_content)) if include_blank?
|
82
|
+
|
83
|
+
items.inject('') do |build, item|
|
84
|
+
label_html_options = item.id ? html_options.merge(value: item.id.to_s) : html_options
|
85
|
+
html_options[:checked] = selected_item == item.id.to_s && !item.new_record?
|
86
|
+
build + form.label(field_id, label_html_options) do
|
87
|
+
form.radio_button(field_id, item.id, html_options) + item.name
|
88
|
+
end
|
89
|
+
end.html_safe
|
90
|
+
else
|
91
|
+
error_message
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def error_message
|
96
|
+
"Could not find system_code of '#{system_code_name}' or '#{form.object_name}_#{system_code_name}'"
|
97
|
+
end
|
98
|
+
|
66
99
|
def selected_item
|
67
100
|
if object.new_record? && object.send(field_id).blank?
|
68
101
|
default_item
|
@@ -74,6 +107,10 @@ module Selections
|
|
74
107
|
def default_item
|
75
108
|
items.where(:is_default => true).first.try(:id).to_s
|
76
109
|
end
|
110
|
+
|
111
|
+
def blank_content
|
112
|
+
options[:blank_content] || 'none'
|
113
|
+
end
|
77
114
|
end
|
78
115
|
|
79
116
|
ActiveSupport.on_load :action_view do
|
data/lib/selections/version.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
include Selections::FormBuilderExtensions
|
4
4
|
|
@@ -225,45 +225,102 @@ describe SelectionTag do
|
|
225
225
|
end
|
226
226
|
end
|
227
227
|
|
228
|
-
describe
|
229
|
-
|
230
|
-
|
231
|
-
expect(edit_form.to_tag).to eq("Could not find system_code of 'priority' or 'ticket_priority'")
|
232
|
-
end
|
233
|
-
it "displays warning for system_code override" do
|
234
|
-
expect(edit_form(options: {system_code: "hello"}).to_tag).to eq("Could not find system_code of 'hello' or 'ticket_hello'")
|
235
|
-
end
|
228
|
+
describe '.blank_content' do
|
229
|
+
it 'when nothing set' do
|
230
|
+
expect(new_form.blank_content).to eq('none')
|
236
231
|
end
|
237
|
-
|
238
|
-
|
232
|
+
it 'when set' do
|
233
|
+
expect(new_form(options: {blank_content: 'hello'}).blank_content).to eq('hello')
|
234
|
+
end
|
235
|
+
end
|
239
236
|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
237
|
+
context 'html output' do
|
238
|
+
describe '.select_tag' do
|
239
|
+
context 'invalid' do
|
240
|
+
it 'displays warning when system_code does not exist' do
|
241
|
+
expect(edit_form.select_tag).to eq("Could not find system_code of 'priority' or 'ticket_priority'")
|
244
242
|
end
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
it("has selection_3 selected") { expect(Nokogiri::HTML(new_form.to_tag).search("option[selected]").first.content).to eq(selection_3.name) }
|
249
|
-
it("has no blank option") { expect(Nokogiri::HTML(new_form.to_tag).search("option[value='']").count).to eq(0) }
|
243
|
+
it 'displays warning for system_code override' do
|
244
|
+
expect(edit_form(options: {system_code: 'hello'}).select_tag).to eq("Could not find system_code of 'hello' or 'ticket_hello'")
|
250
245
|
end
|
251
246
|
end
|
247
|
+
context 'valid system_code' do
|
248
|
+
before { all_selections }
|
252
249
|
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
250
|
+
context 'new form' do
|
251
|
+
context 'no default' do
|
252
|
+
it('has no selected item') { expect(Nokogiri::HTML(new_form.select_tag).search('option[selected]')).to be_empty }
|
253
|
+
it('has a blank option') { expect(Nokogiri::HTML(new_form.select_tag).search("option[value='']").count).to eq(1) }
|
254
|
+
end
|
255
|
+
context 'default is set' do
|
256
|
+
before { selection_3.update_attribute(:is_default, true) }
|
257
|
+
|
258
|
+
it('has selection_3 selected') { expect(Nokogiri::HTML(new_form.select_tag).search('option[selected]').first.content).to eq(selection_3.name) }
|
259
|
+
it('has no blank option') { expect(Nokogiri::HTML(new_form.select_tag).search("option[value='']").count).to eq(0) }
|
260
|
+
end
|
257
261
|
end
|
258
|
-
context "when relation (priority_id) is set to selection_3" do
|
259
|
-
before { ticket.update_attribute(:priority_id, selection_3.id) }
|
260
262
|
|
261
|
-
|
262
|
-
|
263
|
+
context 'edit form' do
|
264
|
+
context 'relation (priority_id) is nil' do
|
265
|
+
it('has no selected item') { expect(Nokogiri::HTML(edit_form.select_tag).search('option[selected]')).to be_empty }
|
266
|
+
it('has a blank option') { expect(Nokogiri::HTML(edit_form.select_tag).search("option[value='']").count).to eq(1) }
|
267
|
+
end
|
268
|
+
context 'when relation (priority_id) is set to selection_3' do
|
269
|
+
before { ticket.update_attribute(:priority_id, selection_3.id) }
|
270
|
+
|
271
|
+
it('item is selected') { expect(Nokogiri::HTML(edit_form.select_tag).search('option[selected]').first.content).to eq(selection_3.name) }
|
272
|
+
it('has no blank option') { expect(Nokogiri::HTML(edit_form.select_tag).search("option[value='']").count).to eq(0) }
|
273
|
+
end
|
274
|
+
end
|
275
|
+
it 'returns valid html' do
|
276
|
+
expect(edit_form.select_tag).to eq "<select id=\"ticket_priority_id\" name=\"ticket[priority_id]\"><option value=\"\"></option>\n<option value=\"4\">high</option>\n<option value=\"2\">low</option>\n<option value=\"3\">medium</option></select>"
|
263
277
|
end
|
264
278
|
end
|
265
|
-
|
266
|
-
|
279
|
+
end
|
280
|
+
describe '.radio_tag' do
|
281
|
+
context 'invalid' do
|
282
|
+
it 'displays warning when system_code does not exist' do
|
283
|
+
expect(edit_form.radio_tag).to eq("Could not find system_code of 'priority' or 'ticket_priority'")
|
284
|
+
end
|
285
|
+
it 'displays warning for system_code override' do
|
286
|
+
expect(edit_form(options: {system_code: "hello"}).radio_tag).to eq("Could not find system_code of 'hello' or 'ticket_hello'")
|
287
|
+
end
|
288
|
+
end
|
289
|
+
context 'valid system_code' do
|
290
|
+
before { all_selections }
|
291
|
+
|
292
|
+
context 'new form' do
|
293
|
+
context 'no default' do
|
294
|
+
it('has no selected item') { expect(Nokogiri::HTML(new_form.radio_tag).search('input[checked]')).to be_empty }
|
295
|
+
it('has a blank option') { expect(Nokogiri::HTML(new_form.radio_tag).search('label').first.content).to eq('none') }
|
296
|
+
it('has a custom blank option') { expect(Nokogiri::HTML(new_form(options: {blank_content: 'hello'}).radio_tag).search('label').first.content).to eq('hello') }
|
297
|
+
end
|
298
|
+
context 'default is set' do
|
299
|
+
before { selection_3.update_attribute(:is_default, true) }
|
300
|
+
|
301
|
+
it('has selection_3 selected') { expect(Nokogiri::HTML(new_form.radio_tag).search('input[checked]').first['value']).to eq(selection_3.id.to_s) }
|
302
|
+
it('has no blank option') { expect(Nokogiri::HTML(new_form.radio_tag).search('label').first.content).to eq(selection_3.name) }
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
context 'edit form' do
|
307
|
+
context 'relation (priority_id) is nil' do
|
308
|
+
it('has no selected item') { expect(Nokogiri::HTML(edit_form.radio_tag).search('input[checked]')).to be_empty }
|
309
|
+
it('has a blank option') { expect(Nokogiri::HTML(edit_form.radio_tag).search('label').first.content).to eq('none') }
|
310
|
+
end
|
311
|
+
context 'when relation (priority_id) is set to selection_3' do
|
312
|
+
before { ticket.update_attribute(:priority_id, selection_3.id) }
|
313
|
+
|
314
|
+
it('item is selected') { expect(Nokogiri::HTML(edit_form.radio_tag).search('input[checked]').first['value']).to eq(selection_3.id.to_s) }
|
315
|
+
it('has no blank option') { expect(Nokogiri::HTML(edit_form.radio_tag).search('label').first.content).to eq(selection_3.name) }
|
316
|
+
it('has a blank option when include_blank set') { expect(Nokogiri::HTML(edit_form(options: {include_blank: true}).radio_tag).search('label').first.content).to eq('none') }
|
317
|
+
end
|
318
|
+
end
|
319
|
+
it 'returns valid html' do
|
320
|
+
ticket.update_attribute(:priority_id, selection_3.id)
|
321
|
+
#p edit_form(options: {:include_blank => true}, html_options: {class: 'fred'}).radio_tag
|
322
|
+
expect(edit_form(options: {:include_blank => true}, html_options: {class: 'fred'}).radio_tag).to eq "<label class=\"fred\" for=\"ticket_priority_id\"><input class=\"fred\" id=\"ticket_priority_id\" name=\"ticket[priority_id]\" type=\"radio\" />none</label><label class=\"fred\" for=\"ticket_priority_id_4\"><input checked=\"checked\" class=\"fred\" id=\"ticket_priority_id_4\" name=\"ticket[priority_id]\" type=\"radio\" value=\"4\" />high</label><label checked=\"checked\" class=\"fred\" for=\"ticket_priority_id_2\"><input class=\"fred\" id=\"ticket_priority_id_2\" name=\"ticket[priority_id]\" type=\"radio\" value=\"2\" />low</label><label class=\"fred\" for=\"ticket_priority_id_3\"><input class=\"fred\" id=\"ticket_priority_id_3\" name=\"ticket[priority_id]\" type=\"radio\" value=\"3\" />medium</label>"
|
323
|
+
end
|
267
324
|
end
|
268
325
|
end
|
269
326
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: selections
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.11
|
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-
|
12
|
+
date: 2013-04-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|