selections 0.0.1 → 0.1.1

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.
Files changed (99) hide show
  1. data/.gitignore +18 -0
  2. data/CHANGELOG.md +3 -0
  3. data/Gemfile +4 -0
  4. data/{MIT-LICENSE → LICENSE.txt} +4 -2
  5. data/README.md +237 -0
  6. data/Rakefile +3 -29
  7. data/lib/selections.rb +22 -6
  8. data/lib/selections/belongs_to_selection.rb +20 -0
  9. data/lib/selections/form_builder_extensions.rb +86 -0
  10. data/lib/selections/selectable.rb +125 -0
  11. data/lib/selections/version.rb +1 -1
  12. data/selections.gemspec +38 -0
  13. data/spec/selections/selectable_spec.rb +220 -0
  14. data/spec/selections/selections_tag_spec.rb +203 -0
  15. data/spec/spec_helper.rb +30 -21
  16. metadata +99 -177
  17. data/README.rdoc +0 -3
  18. data/app/assets/javascripts/selections/application.js +0 -9
  19. data/app/assets/javascripts/selections/selections.js +0 -2
  20. data/app/assets/stylesheets/scaffold.css +0 -56
  21. data/app/assets/stylesheets/selections/application.css +0 -7
  22. data/app/assets/stylesheets/selections/selections.css +0 -4
  23. data/app/controllers/selections/application_controller.rb +0 -4
  24. data/app/controllers/selections/selections_controller.rb +0 -45
  25. data/app/helpers/selections/application_helper.rb +0 -4
  26. data/app/helpers/selections/selectors_helper.rb +0 -4
  27. data/app/models/selections/selection.rb +0 -83
  28. data/app/views/layouts/selections/application.html.erb +0 -1
  29. data/app/views/selections/selections/_form.html.erb +0 -41
  30. data/app/views/selections/selections/edit.html.erb +0 -6
  31. data/app/views/selections/selections/index.html.erb +0 -33
  32. data/app/views/selections/selections/new.html.erb +0 -5
  33. data/app/views/selections/selections/show.html.erb +0 -35
  34. data/config/initializers/selections.rb +0 -1
  35. data/config/routes.rb +0 -5
  36. data/db/migrate/20120114024459_create_selections_selections.rb +0 -15
  37. data/lib/selections/engine.rb +0 -5
  38. data/lib/selections/selection_select.rb +0 -24
  39. data/lib/tasks/selections_tasks.rake +0 -4
  40. data/spec/controllers/companies_controller_spec.rb +0 -10
  41. data/spec/controllers/selections/selections_controller_spec.rb +0 -165
  42. data/spec/dummy/Rakefile +0 -7
  43. data/spec/dummy/app/assets/javascripts/application.js +0 -9
  44. data/spec/dummy/app/assets/javascripts/companies.js +0 -2
  45. data/spec/dummy/app/assets/stylesheets/application.css +0 -7
  46. data/spec/dummy/app/assets/stylesheets/companies.css +0 -4
  47. data/spec/dummy/app/assets/stylesheets/scaffold.css +0 -56
  48. data/spec/dummy/app/controllers/application_controller.rb +0 -2
  49. data/spec/dummy/app/controllers/companies_controller.rb +0 -83
  50. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  51. data/spec/dummy/app/helpers/companies_helper.rb +0 -2
  52. data/spec/dummy/app/models/company.rb +0 -7
  53. data/spec/dummy/app/views/companies/_form.html.erb +0 -3
  54. data/spec/dummy/app/views/companies/edit.html.erb +0 -4
  55. data/spec/dummy/app/views/companies/index.html.erb +0 -27
  56. data/spec/dummy/app/views/companies/new.html.erb +0 -5
  57. data/spec/dummy/app/views/companies/show.html.erb +0 -20
  58. data/spec/dummy/app/views/layouts/application.html.erb +0 -11
  59. data/spec/dummy/config.ru +0 -4
  60. data/spec/dummy/config/application.rb +0 -45
  61. data/spec/dummy/config/boot.rb +0 -10
  62. data/spec/dummy/config/database.yml +0 -25
  63. data/spec/dummy/config/environment.rb +0 -5
  64. data/spec/dummy/config/environments/development.rb +0 -30
  65. data/spec/dummy/config/environments/production.rb +0 -60
  66. data/spec/dummy/config/environments/test.rb +0 -42
  67. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  68. data/spec/dummy/config/initializers/inflections.rb +0 -10
  69. data/spec/dummy/config/initializers/mime_types.rb +0 -5
  70. data/spec/dummy/config/initializers/secret_token.rb +0 -7
  71. data/spec/dummy/config/initializers/session_store.rb +0 -8
  72. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  73. data/spec/dummy/config/locales/en.yml +0 -5
  74. data/spec/dummy/config/routes.rb +0 -6
  75. data/spec/dummy/db/development.sqlite3 +0 -0
  76. data/spec/dummy/db/migrate/20120114125226_create_selections_selections.rb +0 -15
  77. data/spec/dummy/db/migrate/20120115055119_create_companies.rb +0 -12
  78. data/spec/dummy/db/schema.rb +0 -38
  79. data/spec/dummy/db/test.sqlite3 +0 -0
  80. data/spec/dummy/log/development.log +0 -94
  81. data/spec/dummy/log/test.log +0 -26360
  82. data/spec/dummy/public/404.html +0 -26
  83. data/spec/dummy/public/422.html +0 -26
  84. data/spec/dummy/public/500.html +0 -26
  85. data/spec/dummy/public/favicon.ico +0 -0
  86. data/spec/dummy/script/rails +0 -6
  87. data/spec/helpers/selections/selections_helper_spec.rb +0 -15
  88. data/spec/models/company_spec.rb +0 -7
  89. data/spec/models/selections/selection_spec.rb +0 -219
  90. data/spec/routing/selections/selections_routing_spec.rb +0 -35
  91. data/spec/support/controller_route_fix.rb +0 -33
  92. data/spec/support/factories/company_factory.rb +0 -5
  93. data/spec/support/factories/selection_factory.rb +0 -5
  94. data/spec/views/companies/edit.html.erb_spec.rb +0 -76
  95. data/spec/views/companies/new.html.erb_spec.rb +0 -78
  96. data/spec/views/selections/selections/edit.html.erb_spec.rb +0 -28
  97. data/spec/views/selections/selections/index.html.erb_spec.rb +0 -40
  98. data/spec/views/selections/selections/new.html.erb_spec.rb +0 -28
  99. data/spec/views/selections/selections/show.html.erb_spec.rb +0 -30
@@ -1,7 +0,0 @@
1
- /*
2
- * This is a manifest file that'll automatically include all the stylesheets available in this directory
3
- * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
4
- * the top of the compiled file, but it's generally better to create a new file per style scope.
5
- *= require_self
6
- *= require_tree .
7
- */
@@ -1,4 +0,0 @@
1
- /*
2
- Place all the styles related to the matching controller here.
3
- They will automatically be included in application.css.
4
- */
@@ -1,4 +0,0 @@
1
- module Selections
2
- class ApplicationController < ActionController::Base
3
- end
4
- end
@@ -1,45 +0,0 @@
1
- module Selections
2
- class SelectionsController < ApplicationController
3
- def index
4
- @selections = Selection.all
5
- end
6
-
7
- def show
8
- @selection = Selection.find(params[:id])
9
- end
10
-
11
- def new
12
- @selection = Selection.new
13
- end
14
-
15
- def edit
16
- @selection = Selection.find(params[:id])
17
- end
18
-
19
- def create
20
- @selection = Selection.new(params[:selection])
21
- if @selection.save
22
- redirect_to @selection, notice: 'Selection was successfully created.'
23
- else
24
- render action: "new"
25
- end
26
- end
27
-
28
- def update
29
- @selection = Selection.find(params[:id])
30
-
31
- if @selection.update_attributes(params[:selection])
32
- redirect_to @selection, notice: 'Selection was successfully updated.'
33
- else
34
- render action: "edit"
35
- end
36
- end
37
-
38
- def destroy
39
- @selection = Selection.find(params[:id])
40
- @selection.destroy
41
-
42
- redirect_to selections_url
43
- end
44
- end
45
- end
@@ -1,4 +0,0 @@
1
- module Selections
2
- module ApplicationHelper
3
- end
4
- end
@@ -1,4 +0,0 @@
1
- module Selections
2
- module SelectorsHelper
3
- end
4
- end
@@ -1,83 +0,0 @@
1
- module Selections
2
- class Selection < ActiveRecord::Base
3
-
4
- VERSION = "0.1"
5
- HIDDEN_POSITION = 999888777
6
-
7
- acts_as_tree
8
-
9
- validate :name, :existence =>true
10
- validates_presence_of :name
11
- validates_uniqueness_of :name, :scope => :parent_id
12
- validates_uniqueness_of :system_code, :scope => :archived_at
13
- validates_format_of :system_code, :with => /^[a-z][a-zA-Z_0-9]*$/, :message => "can only contain alphanumeric characters and '_', not spaces"
14
-
15
- before_validation :auto_gen_system_code, :on => :create
16
- before_validation :disable_system_code_change, :on => :update
17
- after_validation :check_defaults
18
-
19
- default_scope :order => [:position_value, :name]
20
-
21
- scope :filter_archived_except_selected, lambda { |selected_id| {:conditions => ["archived_at is ? or id = ?", nil, selected_id]} }
22
-
23
- def to_s
24
- name.to_s
25
- end
26
-
27
- def position=(value)
28
- self.position_value = value || HIDDEN_POSITION
29
- end
30
-
31
- def disable_system_code_change
32
- errors.add(:system_code, "cannot be changed") if system_code_changed?
33
- end
34
-
35
- def position
36
- position_value unless position_value == HIDDEN_POSITION
37
- end
38
-
39
- def auto_gen_system_code
40
- unless system_code
41
- if parent
42
- self.system_code = parent.system_code + "_" + name.to_s.underscore.split(" ").join("_").singularize.underscore
43
- else
44
- self.system_code = name.to_s.underscore.split(" ").join("_").singularize.underscore
45
- end
46
- end
47
- end
48
-
49
- def check_defaults
50
- siblings_with_default_set.update_attribute(:is_default, false) if self.parent && siblings_with_default_set && self.is_default
51
- self.is_default = false if archived
52
- end
53
-
54
- def siblings_with_default_set
55
- self.parent.children.where(:is_default => true).where("id != #{self.id.to_i}").first
56
- end
57
-
58
- def
59
- self.method_missing lookup_code, *options
60
- if (scope = Selection.where(:system_code => lookup_code.to_s)) && scope.exists?
61
- scope.first
62
- elsif (scope = Selection.where(:system_code => lookup_code.to_s.singularize)) && scope.exists?
63
- scope.first.children
64
- else
65
- super
66
- end
67
- end
68
-
69
- def archived
70
- !!archived_at
71
- end
72
-
73
- def archived=(archived_set)
74
- if archived_set
75
- self.archived_at = Time.now unless archived_at
76
- else
77
- self.archived_at = nil
78
- end
79
- end
80
-
81
- end
82
- end
83
-
@@ -1 +0,0 @@
1
- <%= yield %>
@@ -1,41 +0,0 @@
1
- <%= form_for(@selection) do |f| %>
2
- <% if @selection.errors.any? %>
3
- <div id="error_explanation">
4
- <h2><%= pluralize(@selection.errors.count, "error") %> prohibited this selection from being saved:</h2>
5
-
6
- <ul>
7
- <% @selection.errors.full_messages.each do |msg| %>
8
- <li><%= msg %></li>
9
- <% end %>
10
- </ul>
11
- </div>
12
- <% end %>
13
-
14
- <div class="field">
15
- <%= f.label :name %><br />
16
- <%= f.text_field :name %>
17
- </div>
18
- <div class="field">
19
- <%= f.label :parent_id %><br />
20
- <%= f.number_field :parent_id %>
21
- </div>
22
- <div class="field">
23
- <%= f.label :system_code %><br />
24
- <%= f.text_field :system_code %>
25
- </div>
26
- <div class="field">
27
- <%= f.label :position %><br />
28
- <%= f.number_field :position %>
29
- </div>
30
- <div class="field">
31
- <%= f.label :is_default %><br />
32
- <%= f.check_box :is_default %>
33
- </div>
34
- <div class="field">
35
- <%= f.label :is_system %><br />
36
- <%= f.check_box :is_system %>
37
- </div>
38
- <div class="actions">
39
- <%= f.submit %>
40
- </div>
41
- <% end %>
@@ -1,6 +0,0 @@
1
- <h1>Editing selection</h1>
2
-
3
- <%= render 'form' %>
4
-
5
- <%= link_to 'Show', @selection %> |
6
- <%= link_to 'Back', selections_path %>
@@ -1,33 +0,0 @@
1
- <h1>Listing selections</h1>
2
-
3
- <table>
4
- <tr>
5
- <th>Name</th>
6
- <th>Parent</th>
7
- <th>System code</th>
8
- <th>Position</th>
9
- <th>Is default</th>
10
- <th>Is system</th>
11
- <th></th>
12
- <th></th>
13
- <th></th>
14
- </tr>
15
-
16
- <% @selections.each do |selection| %>
17
- <tr>
18
- <td><%= selection.name %></td>
19
- <td><%= selection.parent_id %></td>
20
- <td><%= selection.system_code %></td>
21
- <td><%= selection.position %></td>
22
- <td><%= selection.is_default %></td>
23
- <td><%= selection.is_system %></td>
24
- <td><%= link_to 'Show', selection %></td>
25
- <td><%= link_to 'Edit', edit_selection_path(selection) %></td>
26
- <td><%= link_to 'Destroy', selection, confirm: 'Are you sure?', method: :delete %></td>
27
- </tr>
28
- <% end %>
29
- </table>
30
-
31
- <br />
32
-
33
- <%= link_to 'New Selection', new_selection_path %>
@@ -1,5 +0,0 @@
1
- <h1>New selection</h1>
2
-
3
- <%= render 'form' %>
4
-
5
- <%= link_to 'Back', selections_path %>
@@ -1,35 +0,0 @@
1
- <p id="notice"><%= notice %></p>
2
-
3
- <p>
4
- <b>Name:</b>
5
- <%= @selection.name %>
6
- </p>
7
-
8
- <p>
9
- <b>Parent:</b>
10
- <%= @selection.parent_id %>
11
- </p>
12
-
13
- <p>
14
- <b>System code:</b>
15
- <%= @selection.system_code %>
16
- </p>
17
-
18
- <p>
19
- <b>Position:</b>
20
- <%= @selection.position %>
21
- </p>
22
-
23
- <p>
24
- <b>Is default:</b>
25
- <%= @selection.is_default %>
26
- </p>
27
-
28
- <p>
29
- <b>Is system:</b>
30
- <%= @selection.is_system %>
31
- </p>
32
-
33
-
34
- <%= link_to 'Edit', edit_selection_path(@selection) %> |
35
- <%= link_to 'Back', selections_path %>
@@ -1 +0,0 @@
1
- #ApplicationController.helper 'selections/selectors'
@@ -1,5 +0,0 @@
1
- Selections::Engine.routes.draw do
2
- scope :selections do
3
- resources :selections
4
- end
5
- end
@@ -1,15 +0,0 @@
1
- class CreateSelectionsSelections < ActiveRecord::Migration
2
- def change
3
- create_table :selections_selections do |t|
4
- t.string :name
5
- t.integer :parent_id
6
- t.string :system_code
7
- t.integer :position_value, :default => Selections::Selection::HIDDEN_POSITION
8
- t.boolean :is_default, :default => false
9
- t.boolean :is_system, :is_default => false
10
- t.datetime :archived_at
11
-
12
- t.timestamps
13
- end
14
- end
15
- end
@@ -1,5 +0,0 @@
1
- module Selections
2
- class Engine < Rails::Engine
3
- isolate_namespace Selections
4
- end
5
- end
@@ -1,24 +0,0 @@
1
- module SelectionSelect
2
- def selections field, options = {}, html_options = {}
3
- options ||= {}
4
- html_options ||= {}
5
- system_code = options[:system_code] || field
6
- system_code = Selections::Selection.find_by_system_code(system_code.to_s)
7
- if system_code
8
- items = system_code.children
9
- field_id = (field.to_s + "_id").to_sym
10
- if object.new_record? && object.send(field_id).nil?
11
- default = items.find_by_is_default(true)
12
- object.send(field_id.to_s+"=", default.id) if default && !default.archived
13
- end
14
- options[:include_blank] = true if object.send(field_id).blank? && options[:include_blank].nil?
15
- #TODO add default style
16
- #html_options[:style] ||=
17
- select field_id, items.filter_archived_except_selected(object.send(field_id)).map {|item| [item.name, item.id]}, options, html_options
18
- else
19
- "Invalid system_code"
20
- end
21
- end
22
- end
23
-
24
- ActionView::Helpers::FormBuilder.send :include, SelectionSelect
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :selections do
3
- # # Task goes here
4
- # end
@@ -1,10 +0,0 @@
1
- require 'spec_helper'
2
- describe CompaniesController do
3
-
4
- describe "GET new" do
5
- it "assigns a new selection as @selection" do
6
- get :new
7
- assigns(:company).should be_a_new(Company)
8
- end
9
- end
10
- end
@@ -1,165 +0,0 @@
1
- require 'spec_helper'
2
-
3
- # This spec was generated by rspec-rails when you ran the scaffold generator.
4
- # It demonstrates how one might use RSpec to specify the controller code that
5
- # was generated by Rails when you ran the scaffold generator.
6
- #
7
- # It assumes that the implementation code is generated by the rails scaffold
8
- # generator. If you are using any extension libraries to generate different
9
- # controller code, this generated spec may or may not pass.
10
- #
11
- # It only uses APIs available in rails and/or rspec-rails. There are a number
12
- # of tools you can use to make these specs even more expressive, but we're
13
- # sticking to rails and rspec-rails APIs to keep things simple and stable.
14
- #
15
- # Compared to earlier versions of this generator, there is very limited use of
16
- # stubs and message expectations in this spec. Stubs are only used when there
17
- # is no simpler way to get a handle on the object needed for the example.
18
- # Message expectations are only used when there is no simpler way to specify
19
- # that an instance is receiving a specific message.
20
- module Selections
21
- describe SelectionsController do
22
-
23
- # This should return the minimal set of attributes required to create a valid
24
- # Selection. As you add validations to Selection, be sure to
25
- # update the return value of this method accordingly.
26
- def valid_attributes
27
- Factory.build(:selection).attributes
28
- end
29
-
30
- # This should return the minimal set of values that should be in the session
31
- # in order to pass any filters (e.g. authentication) defined in
32
- # SelectionsController. Be sure to keep this updated too.
33
- def valid_session
34
- {}
35
- end
36
-
37
- describe "GET index" do
38
- it "assigns all selections as @selections" do
39
- selection = Selection.create! valid_attributes
40
- get :index, {}, valid_session
41
- assigns(:selections).should eq([selection])
42
- end
43
- end
44
-
45
- describe "GET show" do
46
- it "assigns the requested selection as @selection" do
47
- selection = Selection.create! valid_attributes
48
- get :show, {:id => selection.to_param}, valid_session
49
- assigns(:selection).should eq(selection)
50
- end
51
- end
52
-
53
- describe "GET new" do
54
- it "assigns a new selection as @selection" do
55
- get :new, {}, valid_session
56
- assigns(:selection).should be_a_new(Selection)
57
- end
58
- end
59
-
60
- describe "GET edit" do
61
- it "assigns the requested selection as @selection" do
62
- selection = Selection.create! valid_attributes
63
- get :edit, {:id => selection.to_param}, valid_session
64
- assigns(:selection).should eq(selection)
65
- end
66
- end
67
-
68
- describe "POST create" do
69
- describe "with valid params" do
70
- it "creates a new Selection" do
71
- expect {
72
- post :create, {:selection => valid_attributes}, valid_session
73
- }.to change(Selection, :count).by(1)
74
- end
75
-
76
- it "assigns a newly created selection as @selection" do
77
- post :create, {:selection => valid_attributes}, valid_session
78
- assigns(:selection).should be_a(Selection)
79
- assigns(:selection).should be_persisted
80
- end
81
-
82
- it "redirects to the created selection" do
83
- post :create, {:selection => valid_attributes}, valid_session
84
- response.should redirect_to(Selection.last)
85
- end
86
- end
87
-
88
- describe "with invalid params" do
89
- it "assigns a newly created but unsaved selection as @selection" do
90
- # Trigger the behavior that occurs when invalid params are submitted
91
- Selection.any_instance.stub(:save).and_return(false)
92
- post :create, {:selection => {}}, valid_session
93
- assigns(:selection).should be_a_new(Selection)
94
- end
95
-
96
- it "re-renders the 'new' template" do
97
- # Trigger the behavior that occurs when invalid params are submitted
98
- Selection.any_instance.stub(:save).and_return(false)
99
- post :create, {:selection => {}}, valid_session
100
- response.should render_template("new")
101
- end
102
- end
103
- end
104
-
105
- describe "PUT update" do
106
- describe "with valid params" do
107
- it "updates the requested selection" do
108
- selection = Selection.create! valid_attributes
109
- # Assuming there are no other selections in the database, this
110
- # specifies that the Selection created on the previous line
111
- # receives the :update_attributes message with whatever params are
112
- # submitted in the request.
113
- Selection.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
114
- put :update, {:id => selection.to_param, :selection => {'these' => 'params'}}, valid_session
115
- end
116
-
117
- it "assigns the requested selection as @selection" do
118
- selection = Selection.create! valid_attributes
119
- put :update, {:id => selection.to_param, :selection => valid_attributes}, valid_session
120
- assigns(:selection).should eq(selection)
121
- end
122
-
123
- it "redirects to the selection" do
124
- selection = Selection.create! valid_attributes
125
- put :update, {:id => selection.to_param, :selection => valid_attributes.delete_if{|x| x=="system_code"}}, valid_session
126
- response.should redirect_to(selection)
127
- end
128
- end
129
-
130
- describe "with invalid params" do
131
- it "assigns the selection as @selection" do
132
- selection = Selection.create! valid_attributes
133
- # Trigger the behavior that occurs when invalid params are submitted
134
- Selection.any_instance.stub(:save).and_return(false)
135
- put :update, {:id => selection.to_param, :selection => {}}, valid_session
136
- assigns(:selection).should eq(selection)
137
- end
138
-
139
- it "re-renders the 'edit' template" do
140
- selection = Selection.create! valid_attributes
141
- # Trigger the behavior that occurs when invalid params are submitted
142
- Selection.any_instance.stub(:save).and_return(false)
143
- put :update, {:id => selection.to_param, :selection => {}}, valid_session
144
- response.should render_template("edit")
145
- end
146
- end
147
- end
148
-
149
- describe "DELETE destroy" do
150
- it "destroys the requested selection" do
151
- selection = Selection.create! valid_attributes
152
- expect {
153
- delete :destroy, {:id => selection.to_param}, valid_session
154
- }.to change(Selection, :count).by(-1)
155
- end
156
-
157
- it "redirects to the selections list" do
158
- selection = Selection.create! valid_attributes
159
- delete :destroy, {:id => selection.to_param}, valid_session
160
- response.should redirect_to(selections_url)
161
- end
162
- end
163
-
164
- end
165
- end