selections 0.1.13 → 0.1.14
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 +3 -0
- data/README.md +18 -5
- data/lib/selections/belongs_to_selection.rb +33 -4
- data/lib/selections/version.rb +1 -1
- data/selections.gemspec +1 -1
- data/spec/selections/belongs_to_selection_spec.rb +69 -0
- metadata +20 -3
- data/lib/generators/selections_scaffold/templates/.DS_Store +0 -0
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -10,6 +10,7 @@ Selection list management and form and view helpers.
|
|
10
10
|
- f.selections :priorities # dropdowns
|
11
11
|
- f.radios :priorities # radio buttons
|
12
12
|
* Model helpers for joining tables ( eg. belongs_to_selection :priority )
|
13
|
+
* Matchers eg. @ticket.priority_high?
|
13
14
|
* Handling of archived items ( displaying if selected only )
|
14
15
|
* Ordering of lists based on alpha or numbered
|
15
16
|
* Default item handling
|
@@ -44,19 +45,17 @@ can change the name), and should be generated such as:
|
|
44
45
|
|
45
46
|
```ruby
|
46
47
|
rails generate model Selection name parent_id:integer system_code position_value:integer is_default:boolean is_system:boolean archived_at:datetime
|
47
|
-
```
|
48
48
|
|
49
|
-
### Selection Model
|
50
|
-
And next, edit this class to look like:
|
51
49
|
|
52
|
-
```ruby
|
53
50
|
class Selection < ActiveRecord::Base
|
54
51
|
selectable
|
55
52
|
end
|
56
53
|
```
|
57
54
|
|
55
|
+
## Selections
|
56
|
+
|
58
57
|
Selections table can contain one or many lists, it uses the acts_as_tree so each root is a new list
|
59
|
-
meta example: (see below for example
|
58
|
+
meta example: (see below for example YML file).
|
60
59
|
|
61
60
|
* priority
|
62
61
|
- high
|
@@ -180,6 +179,20 @@ eg: show.html.erb
|
|
180
179
|
</p>
|
181
180
|
```
|
182
181
|
|
182
|
+
## Matchers
|
183
|
+
|
184
|
+
Selections supports lookup using the boolean ? method, as an example:
|
185
|
+
|
186
|
+
instead of needing to do
|
187
|
+
```ruby
|
188
|
+
if @ticket.priority == Selection.ticket_priority_high
|
189
|
+
```
|
190
|
+
you can check directly on the instance:
|
191
|
+
```ruby
|
192
|
+
if @ticket.priority_high?
|
193
|
+
```
|
194
|
+
Thanks to @mattconnolly
|
195
|
+
|
183
196
|
## Automatic Features
|
184
197
|
#### Include Blank
|
185
198
|
|
@@ -3,13 +3,42 @@ module Selections
|
|
3
3
|
|
4
4
|
# Helper for belongs_to and accepts all the standard rails options
|
5
5
|
#
|
6
|
-
#Example
|
7
|
-
#
|
6
|
+
# Example
|
7
|
+
# class Thing < ActiveRecord::Base
|
8
|
+
# belongs_to_selection :priority
|
8
9
|
#
|
9
10
|
# by default adds - class_name: "Selection"
|
10
|
-
|
11
|
+
#
|
12
|
+
# This macro also adds a number of methods onto the class if there is a selection
|
13
|
+
# named as the class underscore name (eg: "thing_priority"), then methods are created
|
14
|
+
# for all of the selection values under that parent. For example:
|
15
|
+
#
|
16
|
+
# thing = Thing.find(x)
|
17
|
+
# thing.priority = Selection.thing_priority_high
|
18
|
+
# thing.priority_high? #=> true
|
19
|
+
# thing.priority_low? #=> false
|
20
|
+
#
|
21
|
+
# thing.priority_high? is equivalent to thing.priority == Selection.thing_priority_high
|
22
|
+
# except that the id of the selection is cached at the time the class is loaded.
|
23
|
+
#
|
24
|
+
# Note that this is only appropriate to use for system selection values that are known
|
25
|
+
# at development time, and not to values that the users can edit in the live system.
|
11
26
|
def belongs_to_selection(target, options={})
|
12
27
|
belongs_to target, options.merge(:class_name => "Selection")
|
28
|
+
|
29
|
+
prefix = self.name.downcase
|
30
|
+
parent = Selection.where(system_code: "#{prefix}_#{target}").first
|
31
|
+
if parent
|
32
|
+
target_id = "#{target}_id".to_sym
|
33
|
+
parent.children.each do |s|
|
34
|
+
method_name = "#{s.system_code.sub("#{prefix}_", '')}?".to_sym
|
35
|
+
class_eval do
|
36
|
+
define_method method_name do
|
37
|
+
send(target_id) == s.id
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
13
42
|
end
|
14
43
|
|
15
44
|
ActiveSupport.on_load :active_record do
|
@@ -17,4 +46,4 @@ module Selections
|
|
17
46
|
end
|
18
47
|
|
19
48
|
end
|
20
|
-
end
|
49
|
+
end
|
data/lib/selections/version.rb
CHANGED
data/selections.gemspec
CHANGED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'shoulda/matchers'
|
3
|
+
|
4
|
+
describe Selections::BelongsToSelection do
|
5
|
+
|
6
|
+
let(:parent) { Selection.create(name: "priority", system_code: "ticket_priority") }
|
7
|
+
let(:selection_1) { Selection.create(name: "low", parent_id: parent.id, system_code: "ticket_priority_low") }
|
8
|
+
let(:selection_2) { Selection.create(name: "medium", parent_id: parent.id, system_code: "ticket_priority_medium") }
|
9
|
+
let(:selection_3) { Selection.create(name: "high", parent_id: parent.id, system_code: "ticket_priority_high") }
|
10
|
+
|
11
|
+
let(:ticket_class) do
|
12
|
+
# create the priority records *before* using belongs_to_selection on the class.
|
13
|
+
selection_1; selection_2; selection_3
|
14
|
+
|
15
|
+
Class.new(ActiveRecord::Base) do
|
16
|
+
self.table_name = "tickets"
|
17
|
+
|
18
|
+
# anonymous class has no name, so fake the "Ticket" name
|
19
|
+
def self.name
|
20
|
+
"Ticket"
|
21
|
+
end
|
22
|
+
|
23
|
+
belongs_to_selection :priority
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
before do
|
28
|
+
ticket_class
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'relationships' do
|
32
|
+
it "creates a belongs to relationship" do
|
33
|
+
expect(ticket_class.new).to belong_to(:priority)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'dynamic methods' do
|
38
|
+
subject { ticket_class.new }
|
39
|
+
%w{low medium high}.each do |p|
|
40
|
+
it "creates the method #priority_#{p}?" do
|
41
|
+
expect(subject.respond_to? "priority_#{p}?".to_sym).to be_true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'high priority' do
|
46
|
+
before { subject.priority = selection_3 }
|
47
|
+
|
48
|
+
it("#priority_high? is true") do
|
49
|
+
expect(subject.priority_high?).to be_true
|
50
|
+
end
|
51
|
+
it("#priority_medium? is false") do
|
52
|
+
expect(subject.priority_medium?).to be_false
|
53
|
+
end
|
54
|
+
it("#priority_low? is false") do
|
55
|
+
expect(subject.priority_low?).to be_false
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'with no matching selections' do
|
60
|
+
it "does not create any methods" do
|
61
|
+
expect(ticket_class).not_to receive(:define_method).with { |x|
|
62
|
+
# the relationship will define this method, but there should be no others.
|
63
|
+
x != :autosave_associated_records_for_wrong
|
64
|
+
}
|
65
|
+
ticket_class.belongs_to_selection :wrong
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
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.14
|
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:
|
12
|
+
date: 2014-02-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -123,6 +123,22 @@ dependencies:
|
|
123
123
|
- - ! '>='
|
124
124
|
- !ruby/object:Gem::Version
|
125
125
|
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: shoulda-matchers
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ~>
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: 2.4.0
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ~>
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: 2.4.0
|
126
142
|
description: ! "Selection list management and form and view helpers.\n Manages one
|
127
143
|
table to hold all selections items/dropdown lists ( tree ).\n Dynamic lookup to
|
128
144
|
find parent or children ( eg. Selection.priorities ).\n Form helper to display lists
|
@@ -141,7 +157,6 @@ files:
|
|
141
157
|
- README.md
|
142
158
|
- Rakefile
|
143
159
|
- lib/generators/selections_scaffold/selections_scaffold_generator.rb
|
144
|
-
- lib/generators/selections_scaffold/templates/.DS_Store
|
145
160
|
- lib/generators/selections_scaffold/templates/_form.html.haml
|
146
161
|
- lib/generators/selections_scaffold/templates/create_selections.rb
|
147
162
|
- lib/generators/selections_scaffold/templates/edit.html.erb
|
@@ -162,6 +177,7 @@ files:
|
|
162
177
|
- lib/selections/selectable.rb
|
163
178
|
- lib/selections/version.rb
|
164
179
|
- selections.gemspec
|
180
|
+
- spec/selections/belongs_to_selection_spec.rb
|
165
181
|
- spec/selections/selectable_spec.rb
|
166
182
|
- spec/selections/selections_tag_spec.rb
|
167
183
|
- spec/spec_helper.rb
|
@@ -193,6 +209,7 @@ summary: Selection list management and form and view helpers. Manages one table
|
|
193
209
|
or children ( eg. Selection.priorities ). Form helper to display lists ( eg. f.selections
|
194
210
|
:priorities ). Handling of archived items ( displaying if selected only ).
|
195
211
|
test_files:
|
212
|
+
- spec/selections/belongs_to_selection_spec.rb
|
196
213
|
- spec/selections/selectable_spec.rb
|
197
214
|
- spec/selections/selections_tag_spec.rb
|
198
215
|
- spec/spec_helper.rb
|
Binary file
|