selections 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -35,7 +35,7 @@ can change the name), and should be generated such as:
35
35
  rails generate model Selection name parent_id:integer system_code position_value:integer is_default:boolean is_system:boolean archived_at:datetime
36
36
  ```
37
37
 
38
- ### Model
38
+ ### Selection Model
39
39
  And next, edit this class to look like:
40
40
 
41
41
  ```ruby
@@ -66,7 +66,7 @@ Selection.user_role
66
66
  Dynamic lookups support pluralization and will then return the children:
67
67
 
68
68
  ```ruby
69
- Selection.priorities => [high,med,low] records
69
+ Selection.priorities -> [high,med,low] records
70
70
  ```
71
71
 
72
72
  #### Form Helper
@@ -97,23 +97,48 @@ within the _form.html.erb just use the selections helper method:
97
97
  <% end %>
98
98
  ```
99
99
 
100
- If you have naming conflicts/duplicates eg. user categories and ticket categories within in the selections name the parent/system_code
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
+ ### Form Helper Options
102
101
 
103
- user form file
102
+ ```ruby
103
+ f.selections :fieldname, options = {}, html_options = {}
104
+ ```
105
+
106
+ The selections method excepts all the standard Ruby on Rails form helper options and html formatting - http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select
107
+
108
+ #### System Code Override
109
+
110
+ 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.
104
111
 
105
112
  ```ruby
106
- f.selections :category
113
+ <%= f.selections :variety, :system_code => :category %>
107
114
  ```
108
115
 
109
- this will automatically look up the user_category selections.
116
+ ### Scoped System Code
117
+
118
+ If you have naming conflicts/duplicates system_codes (parent codes) eg. category_id field for user and ticket models
110
119
 
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.
120
+ Selections Data example
121
+
122
+ * user_category
123
+ - civilian
124
+ - programmer
125
+ * ticket_category
126
+ - high
127
+ - low
128
+
129
+ The fieldname within the user and ticket form can both be :category as the form helper will search for
130
+ either 'category' or 'user_category' in the selections model.
131
+
132
+ users/_form.html.erb
112
133
 
113
134
  ```ruby
114
- f.selections :variety, :system_code => :category
135
+ <%= f.selections :category %>
115
136
  ```
116
137
 
138
+ This will automatically look up the 'user_category' selections if 'category' does not exist
139
+
140
+ ## Related Models
141
+
117
142
  Next, you need to tell models that have a selectable association. These are `belongs_to` associations
118
143
  which pull their values from the selections model. Assuming you have a `Ticket` model with a foreign key of priority_id,
119
144
  you can set this up like so:
@@ -130,18 +155,19 @@ From an instance of a ticket within a view the belongs_to selection will return
130
155
  eg: show.html.erb
131
156
 
132
157
  ```ruby
133
- ticket.priority.try(:name) # don't need to do this
134
-
135
- ticket.priority # will default to return name
158
+ <p>
159
+ <b>Priority:</b>
160
+ <%= @ticket.priority %> # Instead of this @ticket.priority.try(:name)
161
+ </p>
136
162
  ```
137
163
 
138
- ### Automatic Features
164
+ ## Automatic Features
139
165
  #### Include Blank
140
166
 
141
167
  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
142
168
  will be no blank row and the default item will be selected.
143
169
 
144
- When editing a form, by default the blank row will not be displayed.
170
+ When editing a form, by default the blank row will not be displayed. Use the options :include_blank => true option to overrided.
145
171
 
146
172
  #### Archived Item
147
173
 
@@ -154,6 +180,9 @@ eg. A ticket has a priority set to high, later the high selection list item was
154
180
 
155
181
  The list will by default display in alphanumeric order, unless you add position values and then it will display in the values.
156
182
 
183
+ #### system_code generation
184
+ On creation of a new item in Selections the system_code field will be automatically generated based on parent and item name if left blank.
185
+
157
186
  ## Configuration
158
187
 
159
188
  If you use a class name other than `Selection` as your selection model, you must
@@ -167,6 +196,7 @@ Selections.model { YourSelectionModel }
167
196
 
168
197
  * Add model generators
169
198
  * Add selections management scaffold/generator
199
+ * Add Radio Button support
170
200
 
171
201
  ## Contributing
172
202
 
@@ -176,7 +206,7 @@ Selections.model { YourSelectionModel }
176
206
  4. Push to the branch (`git push origin my-new-feature`)
177
207
  5. Create new Pull Request
178
208
 
179
- ## example selections.yml
209
+ ## example selections.yml to import into DB
180
210
 
181
211
  ```yaml
182
212
  priority:
@@ -43,8 +43,8 @@ module Selections
43
43
  end
44
44
 
45
45
  def system_code
46
- @system_code ||= selection.where(system_code: system_code_name.to_s).first
47
46
  @system_code ||= selection.where(system_code: "#{form.object_name}_#{system_code_name}").first
47
+ @system_code ||= selection.where(system_code: system_code_name.to_s).first
48
48
  end
49
49
 
50
50
  def items
@@ -59,7 +59,7 @@ module Selections
59
59
  options[:selected] = selected_item
60
60
  form.select field_id, items.map { |item| [item.name, item.id] }, options, html_options
61
61
  else
62
- "Invalid system_code of '#{system_code_name}'"
62
+ "Could not find system_code of '#{system_code_name}' or '#{form.object_name}_#{system_code_name}'"
63
63
  end
64
64
  end
65
65
 
@@ -1,3 +1,3 @@
1
1
  module Selections
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -41,6 +41,13 @@ describe SelectionTag do
41
41
  before { parent }
42
42
  it("returns priority selection item") { expect(edit_form.system_code).to eq(parent) }
43
43
  it("does not find") { expect(edit_form(field: "non_existent").system_code).to be_nil }
44
+ context "when more explicit route should found first" do
45
+ before { model_parent }
46
+ it("should find more explicit route of model parent") { expect(edit_form.system_code).to eq(model_parent) }
47
+ it "should use priority system_code when model is not ticket" do
48
+ expect(edit_form(form: ActionView::Helpers::FormBuilder.new(:user, :user, ActionView::Base.new, {}, Proc.new {}) ).system_code).to eq(parent)
49
+ end
50
+ end
44
51
  end
45
52
 
46
53
  it "finds with form model prefixed" do
@@ -178,8 +185,13 @@ describe SelectionTag do
178
185
  end
179
186
 
180
187
  describe ".to_tag" do
181
- it "displays warning when system_code does not exist" do
182
- expect(edit_form.to_tag).to eq("Invalid system_code of 'priority'")
188
+ context "invalid" do
189
+ it "displays warning when system_code does not exist" do
190
+ expect(edit_form.to_tag).to eq("Could not find system_code of 'priority' or 'ticket_priority'")
191
+ end
192
+ it "displays warning for system_code override" do
193
+ expect(edit_form(options: {system_code: "hello"}).to_tag).to eq("Could not find system_code of 'hello' or 'ticket_hello'")
194
+ end
183
195
  end
184
196
  context "valid system_code" do
185
197
  before { all_selections }
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.3
4
+ version: 0.1.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: 2012-11-26 00:00:00.000000000 Z
12
+ date: 2012-12-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord