selections 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +9 -1
- data/README.md +26 -23
- data/lib/selections/form_builder_extensions.rb +1 -1
- data/lib/selections/version.rb +1 -1
- data/spec/selections/selections_tag_spec.rb +62 -48
- metadata +2 -2
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -45,24 +45,25 @@ end
|
|
45
45
|
```
|
46
46
|
|
47
47
|
Selections table can contain one or many lists, it uses the acts_as_tree so each root is a new list
|
48
|
-
meta example: (see below for example YAML file)
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
48
|
+
meta example: (see below for example YAML file).
|
49
|
+
|
50
|
+
* priority
|
51
|
+
- high
|
52
|
+
- medium
|
53
|
+
- low
|
54
|
+
* user_role
|
55
|
+
- admin
|
56
|
+
- owner
|
57
|
+
- user
|
58
|
+
|
59
|
+
From the rails console it is be possible to access these items directly using dynamic lookups:
|
59
60
|
|
60
61
|
```ruby
|
61
62
|
Selection.priority => returns the parent row
|
62
63
|
Selection.user_role
|
63
64
|
```
|
64
65
|
|
65
|
-
Dynamic lookups support pluralization and will then return the children
|
66
|
+
Dynamic lookups support pluralization and will then return the children:
|
66
67
|
|
67
68
|
```ruby
|
68
69
|
Selection.priorities => [high,med,low] records
|
@@ -70,11 +71,12 @@ Selection.priorities => [high,med,low] records
|
|
70
71
|
|
71
72
|
#### Form Helper
|
72
73
|
|
73
|
-
if we had a controller for Ticket model with fields of
|
74
|
-
|
75
|
-
|
74
|
+
if we had a controller for Ticket model with fields of:
|
75
|
+
|
76
|
+
* name
|
77
|
+
* priority_id
|
76
78
|
|
77
|
-
within the _form.html.erb just use the selections helper method
|
79
|
+
within the _form.html.erb just use the selections helper method:
|
78
80
|
|
79
81
|
```ruby
|
80
82
|
<%= form_for(@ticket) do |f| %>
|
@@ -98,13 +100,15 @@ within the _form.html.erb just use the selections helper method
|
|
98
100
|
If you have naming conflicts/duplicates eg. user categories and ticket categories within in the selections name the parent/system_code
|
99
101
|
user_category & ticket_category. The foreign key within the user and ticket can both be category_id and the form_helper will still be
|
100
102
|
|
101
|
-
user form
|
103
|
+
user form file
|
104
|
+
|
102
105
|
```ruby
|
103
106
|
f.selections :category
|
104
107
|
```
|
108
|
+
|
105
109
|
this will automatically look up the user_category selections.
|
106
110
|
|
107
|
-
If you have a selection named differently to the foreign key eg the foreign key is variety_id, you can use a system_code option
|
111
|
+
If you have a selection named differently to the foreign key eg the foreign key is variety_id, you can use a system_code option.
|
108
112
|
|
109
113
|
```ruby
|
110
114
|
f.selections :variety, :system_code => :category
|
@@ -125,7 +129,6 @@ end
|
|
125
129
|
From an instance of a ticket within a view the belongs_to selection will return string (to_s).
|
126
130
|
eg: show.html.erb
|
127
131
|
|
128
|
-
|
129
132
|
```ruby
|
130
133
|
ticket.priority.try(:name) # don't need to do this
|
131
134
|
|
@@ -136,16 +139,16 @@ eg: show.html.erb
|
|
136
139
|
#### Include Blank
|
137
140
|
|
138
141
|
In a new form the selections list will have blank top row unless a default item is set in the selections eg. Medium Priority, then there
|
139
|
-
will be no blank row and the default item will be selected
|
142
|
+
will be no blank row and the default item will be selected.
|
140
143
|
|
141
|
-
When editing a form, by default the blank row will not be displayed
|
144
|
+
When editing a form, by default the blank row will not be displayed.
|
142
145
|
|
143
146
|
#### Archived Item
|
144
147
|
|
145
148
|
On a new form items that are archived in Selections will not appear in the selection list. When editing an existing record the list will only
|
146
149
|
contain un-archived items, unless the item selected is archived it will then appear.
|
147
150
|
|
148
|
-
eg. A ticket has a priority set to high, later the high selection list item was archived, when we edit the ticket record again the item will show in the list even though it is archived
|
151
|
+
eg. A ticket has a priority set to high, later the high selection list item was archived, when we edit the ticket record again the item will show in the list even though it is archived.
|
149
152
|
|
150
153
|
#### Order
|
151
154
|
|
@@ -173,7 +176,7 @@ Selections.model { YourSelectionModel }
|
|
173
176
|
4. Push to the branch (`git push origin my-new-feature`)
|
174
177
|
5. Create new Pull Request
|
175
178
|
|
176
|
-
##
|
179
|
+
## example selections.yml
|
177
180
|
|
178
181
|
```yaml
|
179
182
|
priority:
|
@@ -22,7 +22,7 @@ module Selections
|
|
22
22
|
class SelectionTag #:nodoc:
|
23
23
|
attr_reader :form, :object, :field, :html_options, :options, :selection, :field_id, :system_code_name
|
24
24
|
|
25
|
-
def initialize(form, object, field,
|
25
|
+
def initialize(form, object, field, options, html_options)
|
26
26
|
@form = form
|
27
27
|
@object = object
|
28
28
|
@field = field
|
data/lib/selections/version.rb
CHANGED
@@ -1,7 +1,25 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
|
4
3
|
include Selections::FormBuilderExtensions
|
4
|
+
|
5
|
+
def edit_form args = {}
|
6
|
+
args[:object] ||= ticket
|
7
|
+
common_form args
|
8
|
+
end
|
9
|
+
|
10
|
+
def new_form args = {}
|
11
|
+
args[:object] ||= Ticket.new
|
12
|
+
common_form args
|
13
|
+
end
|
14
|
+
|
15
|
+
def common_form args = {}
|
16
|
+
args[:form] ||= form
|
17
|
+
args[:field] ||= :priority
|
18
|
+
args[:html_options] ||= {}
|
19
|
+
args[:options] ||= {}
|
20
|
+
SelectionTag.new(args[:form], args[:object], args[:field], args[:options], args[:html_options])
|
21
|
+
end
|
22
|
+
|
5
23
|
describe SelectionTag do
|
6
24
|
let(:parent) { Selection.create(name: "priority") }
|
7
25
|
let(:model_parent) { Selection.create(name: "ticket_priority") }
|
@@ -21,17 +39,13 @@ describe SelectionTag do
|
|
21
39
|
describe ".system_code" do
|
22
40
|
context "direct parent" do
|
23
41
|
before { parent }
|
24
|
-
it
|
25
|
-
|
26
|
-
end
|
27
|
-
it "does not find" do
|
28
|
-
expect(SelectionTag.new(form, ticket, :non_existent, {}, {}).system_code).to be_nil
|
29
|
-
end
|
42
|
+
it("returns priority selection item") { expect(edit_form.system_code).to eq(parent) }
|
43
|
+
it("does not find") { expect(edit_form(field: "non_existent").system_code).to be_nil }
|
30
44
|
end
|
31
45
|
|
32
46
|
it "finds with form model prefixed" do
|
33
47
|
model_parent
|
34
|
-
expect(
|
48
|
+
expect(edit_form.system_code).to eq(model_parent)
|
35
49
|
end
|
36
50
|
end
|
37
51
|
|
@@ -39,28 +53,28 @@ describe SelectionTag do
|
|
39
53
|
before { all_selections }
|
40
54
|
|
41
55
|
it "returns all children items" do
|
42
|
-
expect(
|
56
|
+
expect(edit_form.items.all).to eq(parent.children)
|
43
57
|
end
|
44
58
|
context "archived" do
|
45
59
|
before { selection_2.update_attribute(:archived, "1") }
|
46
60
|
it "returns only non archived items" do
|
47
|
-
expect(
|
61
|
+
expect(edit_form.items).to eq(parent.children - [selection_2])
|
48
62
|
end
|
49
63
|
it "returns archived items when selected" do
|
50
64
|
ticket.update_attribute(:priority_id, selection_2.id)
|
51
|
-
expect(
|
65
|
+
expect(edit_form.items.all).to eq(parent.children)
|
52
66
|
end
|
53
67
|
end
|
54
68
|
end
|
55
69
|
|
56
70
|
describe ".field_id" do
|
57
|
-
it("when string") { expect(
|
58
|
-
it("when symbol") { expect(
|
71
|
+
it("when string") { expect(edit_form(field: "priority").field_id).to eq(:priority_id) }
|
72
|
+
it("when symbol") { expect(edit_form(field: :priority).field_id).to eq(:priority_id) }
|
59
73
|
end
|
60
74
|
|
61
75
|
describe ".system_code_name" do
|
62
|
-
it("sets to field name") { expect(
|
63
|
-
it("when override system_code") { expect(
|
76
|
+
it("sets to field name") { expect(edit_form.system_code_name).to eq(:priority) }
|
77
|
+
it("when override system_code") { expect(edit_form(field: :hello, options: {system_code: :priority}).system_code_name).to eq(:priority) }
|
64
78
|
end
|
65
79
|
|
66
80
|
context "include blank" do
|
@@ -68,63 +82,63 @@ describe SelectionTag do
|
|
68
82
|
|
69
83
|
context "when not set" do
|
70
84
|
context "new form" do
|
71
|
-
it("has blank") { expect(
|
85
|
+
it("has blank") { expect(new_form.include_blank?).to be_true }
|
72
86
|
it "has no blank when default set" do
|
73
87
|
selection_1.update_attribute(:is_default, true)
|
74
|
-
expect(
|
88
|
+
expect(new_form.include_blank?).to be_false
|
75
89
|
end
|
76
90
|
end
|
77
91
|
context "edit form" do
|
78
92
|
it("has no blank when ticket.priority_id is set") do
|
79
93
|
ticket.update_attribute(:priority_id, selection_3.id)
|
80
|
-
expect(
|
94
|
+
expect(edit_form.include_blank?).to be_false
|
81
95
|
end
|
82
|
-
it("has blank when ticket.priority_id is nil") { expect(
|
96
|
+
it("has blank when ticket.priority_id is nil") { expect(edit_form.include_blank?).to be_true }
|
83
97
|
it "has no blank when ticket.priority_id is nil and default set" do
|
84
98
|
selection_1.update_attribute(:is_default, true)
|
85
|
-
expect(
|
99
|
+
expect(edit_form.include_blank?).to be_false
|
86
100
|
end
|
87
101
|
end
|
88
102
|
end
|
89
103
|
|
90
104
|
context "when set false" do
|
91
105
|
context "new form" do
|
92
|
-
it("has no blank") { expect(
|
106
|
+
it("has no blank") { expect(new_form(options: {include_blank: false}).include_blank?).to be_false }
|
93
107
|
it "has no blank when default set" do
|
94
108
|
selection_1.update_attribute(:is_default, true)
|
95
|
-
expect(
|
109
|
+
expect(new_form(options: {include_blank: false}).include_blank?).to be_false
|
96
110
|
end
|
97
111
|
end
|
98
112
|
context "edit form" do
|
99
113
|
it("has no blank when ticket.priority_id is set") do
|
100
114
|
ticket.update_attribute(:priority_id, selection_3.id)
|
101
|
-
expect(
|
115
|
+
expect(edit_form(options: {include_blank: false}).include_blank?).to be_false
|
102
116
|
end
|
103
|
-
it("has no blank even when ticket.priority_id is nil") { expect(
|
117
|
+
it("has no blank even when ticket.priority_id is nil") { expect(edit_form(options: {include_blank: false}).include_blank?).to be_false }
|
104
118
|
it "has no blank when ticket.priority_id is nil and default set" do
|
105
119
|
selection_1.update_attribute(:is_default, true)
|
106
|
-
expect(
|
120
|
+
expect(edit_form(options: {include_blank: false}).include_blank?).to be_false
|
107
121
|
end
|
108
122
|
end
|
109
123
|
end
|
110
124
|
|
111
125
|
context "when set to true" do
|
112
126
|
context "new form" do
|
113
|
-
it("has blank") { expect(
|
127
|
+
it("has blank") { expect(new_form(options: {include_blank: true}).include_blank?).to be_true }
|
114
128
|
it "has blank even when default set" do
|
115
129
|
selection_1.update_attribute(:is_default, true)
|
116
|
-
expect(
|
130
|
+
expect(new_form(options: {include_blank: true}).include_blank?).to be_true
|
117
131
|
end
|
118
132
|
end
|
119
133
|
context "edit form" do
|
120
134
|
it("has blank even when ticket.priority_id is set") do
|
121
135
|
ticket.update_attribute(:priority_id, selection_3.id)
|
122
|
-
expect(
|
136
|
+
expect(edit_form(options: {include_blank: true}).include_blank?).to be_true
|
123
137
|
end
|
124
|
-
it("has blank even when ticket.priority_id is nil") { expect(
|
138
|
+
it("has blank even when ticket.priority_id is nil") { expect(edit_form(options: {include_blank: true}).include_blank?).to be_true }
|
125
139
|
it "has blank even when ticket.priority_id is nil and default set" do
|
126
140
|
selection_1.update_attribute(:is_default, true)
|
127
|
-
expect(
|
141
|
+
expect(edit_form(options: {include_blank: true}).include_blank?).to be_true
|
128
142
|
end
|
129
143
|
end
|
130
144
|
end
|
@@ -133,11 +147,11 @@ describe SelectionTag do
|
|
133
147
|
context ".default_item" do
|
134
148
|
before { all_selections }
|
135
149
|
it "returns nil when no default set" do
|
136
|
-
expect(
|
150
|
+
expect(new_form.default_item).to be_blank
|
137
151
|
end
|
138
152
|
it "should set to default item" do
|
139
153
|
selection_2.update_attribute(:is_default, true)
|
140
|
-
expect(
|
154
|
+
expect(new_form.default_item).to eq(selection_2.id.to_s)
|
141
155
|
end
|
142
156
|
end
|
143
157
|
|
@@ -145,58 +159,58 @@ describe SelectionTag do
|
|
145
159
|
before { all_selections }
|
146
160
|
|
147
161
|
context "when default not set" do
|
148
|
-
it("when new form") { expect(
|
162
|
+
it("when new form") { expect(new_form.selected_item).to eq("") }
|
149
163
|
it "when edit form with ticket.priority_id set" do
|
150
164
|
ticket.update_attribute(:priority_id, selection_3.id)
|
151
|
-
expect(
|
165
|
+
expect(edit_form.selected_item).to eq(selection_3.id.to_s)
|
152
166
|
end
|
153
|
-
it("when edit form with no ticket.priority_id set") { expect(
|
167
|
+
it("when edit form with no ticket.priority_id set") { expect(edit_form.selected_item).to eq("") }
|
154
168
|
end
|
155
169
|
context "when default is set" do
|
156
170
|
before { selection_2.update_attribute(:is_default, true) }
|
157
|
-
it("when new form") { expect(
|
171
|
+
it("when new form") { expect(new_form.selected_item).to eq(selection_2.id.to_s) }
|
158
172
|
it "when edit form with ticket.priority_id set" do
|
159
173
|
ticket.update_attribute(:priority_id, selection_3.id)
|
160
|
-
expect(
|
174
|
+
expect(edit_form.selected_item).to eq(selection_3.id.to_s)
|
161
175
|
end
|
162
|
-
it("when edit form with no ticket.priority_id set") { expect(
|
176
|
+
it("when edit form with no ticket.priority_id set") { expect(edit_form.selected_item).to eq("") }
|
163
177
|
end
|
164
178
|
end
|
165
179
|
|
166
180
|
describe ".to_tag" do
|
167
181
|
it "displays warning when system_code does not exist" do
|
168
|
-
expect(
|
182
|
+
expect(edit_form.to_tag).to eq("Invalid system_code of 'priority'")
|
169
183
|
end
|
170
184
|
context "valid system_code" do
|
171
185
|
before { all_selections }
|
172
186
|
|
173
187
|
context "new form" do
|
174
188
|
context "no default" do
|
175
|
-
it("has no selected item") { expect(Nokogiri::HTML(
|
176
|
-
it("has a blank option") { expect(Nokogiri::HTML(
|
189
|
+
it("has no selected item") { expect(Nokogiri::HTML(new_form.to_tag).search("option[selected]")).to be_empty }
|
190
|
+
it("has a blank option") { expect(Nokogiri::HTML(new_form.to_tag).search("option[value='']").count).to eq(1) }
|
177
191
|
end
|
178
192
|
context "default is set" do
|
179
193
|
before { selection_3.update_attribute(:is_default, true) }
|
180
194
|
|
181
|
-
it("has selection_3 selected") { expect(Nokogiri::HTML(
|
182
|
-
it("has no blank option") { expect(Nokogiri::HTML(
|
195
|
+
it("has selection_3 selected") { expect(Nokogiri::HTML(new_form.to_tag).search("option[selected]").first.content).to eq(selection_3.name) }
|
196
|
+
it("has no blank option") { expect(Nokogiri::HTML(new_form.to_tag).search("option[value='']").count).to eq(0) }
|
183
197
|
end
|
184
198
|
end
|
185
199
|
|
186
200
|
context "edit form" do
|
187
201
|
context "relation (priority_id) is nil" do
|
188
|
-
it("has no selected item") { expect(Nokogiri::HTML(
|
189
|
-
it("has a blank option") { expect(Nokogiri::HTML(
|
202
|
+
it("has no selected item") { expect(Nokogiri::HTML(edit_form.to_tag).search("option[selected]")).to be_empty }
|
203
|
+
it("has a blank option") { expect(Nokogiri::HTML(edit_form.to_tag).search("option[value='']").count).to eq(1) }
|
190
204
|
end
|
191
205
|
context "when relation (priority_id) is set to selection_3" do
|
192
206
|
before { ticket.update_attribute(:priority_id, selection_3.id) }
|
193
207
|
|
194
|
-
it("item is selected") { expect(Nokogiri::HTML(
|
195
|
-
it("has no blank option") { expect(Nokogiri::HTML(
|
208
|
+
it("item is selected") { expect(Nokogiri::HTML(edit_form.to_tag).search("option[selected]").first.content).to eq(selection_3.name) }
|
209
|
+
it("has no blank option") { expect(Nokogiri::HTML(edit_form.to_tag).search("option[value='']").count).to eq(0) }
|
196
210
|
end
|
197
211
|
end
|
198
212
|
it "returns valid html" do
|
199
|
-
expect(
|
213
|
+
expect(edit_form.to_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>"
|
200
214
|
end
|
201
215
|
end
|
202
216
|
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.2
|
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: 2012-11-
|
12
|
+
date: 2012-11-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|