selections 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +3 -0
  3. data/Rakefile +31 -0
  4. data/app/assets/javascripts/selections/application.js +9 -0
  5. data/app/assets/javascripts/selections/selections.js +2 -0
  6. data/app/assets/stylesheets/scaffold.css +56 -0
  7. data/app/assets/stylesheets/selections/application.css +7 -0
  8. data/app/assets/stylesheets/selections/selections.css +4 -0
  9. data/app/controllers/selections/application_controller.rb +4 -0
  10. data/app/controllers/selections/selections_controller.rb +45 -0
  11. data/app/helpers/selections/application_helper.rb +4 -0
  12. data/app/helpers/selections/selectors_helper.rb +4 -0
  13. data/app/models/selections/selection.rb +83 -0
  14. data/app/views/layouts/selections/application.html.erb +1 -0
  15. data/app/views/selections/selections/_form.html.erb +41 -0
  16. data/app/views/selections/selections/edit.html.erb +6 -0
  17. data/app/views/selections/selections/index.html.erb +33 -0
  18. data/app/views/selections/selections/new.html.erb +5 -0
  19. data/app/views/selections/selections/show.html.erb +35 -0
  20. data/config/initializers/selections.rb +1 -0
  21. data/config/routes.rb +5 -0
  22. data/db/migrate/20120114024459_create_selections_selections.rb +15 -0
  23. data/lib/selections.rb +7 -0
  24. data/lib/selections/engine.rb +5 -0
  25. data/lib/selections/selection_select.rb +24 -0
  26. data/lib/selections/version.rb +3 -0
  27. data/lib/tasks/selections_tasks.rake +4 -0
  28. data/spec/controllers/companies_controller_spec.rb +10 -0
  29. data/spec/controllers/selections/selections_controller_spec.rb +165 -0
  30. data/spec/dummy/Rakefile +7 -0
  31. data/spec/dummy/app/assets/javascripts/application.js +9 -0
  32. data/spec/dummy/app/assets/javascripts/companies.js +2 -0
  33. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  34. data/spec/dummy/app/assets/stylesheets/companies.css +4 -0
  35. data/spec/dummy/app/assets/stylesheets/scaffold.css +56 -0
  36. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  37. data/spec/dummy/app/controllers/companies_controller.rb +83 -0
  38. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  39. data/spec/dummy/app/helpers/companies_helper.rb +2 -0
  40. data/spec/dummy/app/models/company.rb +7 -0
  41. data/spec/dummy/app/views/companies/_form.html.erb +3 -0
  42. data/spec/dummy/app/views/companies/edit.html.erb +4 -0
  43. data/spec/dummy/app/views/companies/index.html.erb +27 -0
  44. data/spec/dummy/app/views/companies/new.html.erb +5 -0
  45. data/spec/dummy/app/views/companies/show.html.erb +20 -0
  46. data/spec/dummy/app/views/layouts/application.html.erb +11 -0
  47. data/spec/dummy/config.ru +4 -0
  48. data/spec/dummy/config/application.rb +45 -0
  49. data/spec/dummy/config/boot.rb +10 -0
  50. data/spec/dummy/config/database.yml +25 -0
  51. data/spec/dummy/config/environment.rb +5 -0
  52. data/spec/dummy/config/environments/development.rb +30 -0
  53. data/spec/dummy/config/environments/production.rb +60 -0
  54. data/spec/dummy/config/environments/test.rb +42 -0
  55. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  56. data/spec/dummy/config/initializers/inflections.rb +10 -0
  57. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  58. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  59. data/spec/dummy/config/initializers/session_store.rb +8 -0
  60. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  61. data/spec/dummy/config/locales/en.yml +5 -0
  62. data/spec/dummy/config/routes.rb +6 -0
  63. data/spec/dummy/db/development.sqlite3 +0 -0
  64. data/spec/dummy/db/migrate/20120114125226_create_selections_selections.rb +15 -0
  65. data/spec/dummy/db/migrate/20120115055119_create_companies.rb +12 -0
  66. data/spec/dummy/db/schema.rb +38 -0
  67. data/spec/dummy/db/test.sqlite3 +0 -0
  68. data/spec/dummy/log/development.log +94 -0
  69. data/spec/dummy/log/test.log +26360 -0
  70. data/spec/dummy/public/404.html +26 -0
  71. data/spec/dummy/public/422.html +26 -0
  72. data/spec/dummy/public/500.html +26 -0
  73. data/spec/dummy/public/favicon.ico +0 -0
  74. data/spec/dummy/script/rails +6 -0
  75. data/spec/helpers/selections/selections_helper_spec.rb +15 -0
  76. data/spec/models/company_spec.rb +7 -0
  77. data/spec/models/selections/selection_spec.rb +219 -0
  78. data/spec/routing/selections/selections_routing_spec.rb +35 -0
  79. data/spec/spec_helper.rb +30 -0
  80. data/spec/support/controller_route_fix.rb +33 -0
  81. data/spec/support/factories/company_factory.rb +5 -0
  82. data/spec/support/factories/selection_factory.rb +5 -0
  83. data/spec/views/companies/edit.html.erb_spec.rb +76 -0
  84. data/spec/views/companies/new.html.erb_spec.rb +78 -0
  85. data/spec/views/selections/selections/edit.html.erb_spec.rb +28 -0
  86. data/spec/views/selections/selections/index.html.erb_spec.rb +40 -0
  87. data/spec/views/selections/selections/new.html.erb_spec.rb +28 -0
  88. data/spec/views/selections/selections/show.html.erb_spec.rb +30 -0
  89. metadata +260 -0
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ = Selections
2
+
3
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Selections'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+ Bundler::GemHelper.install_tasks
28
+
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec)
31
+ task :default => :spec
@@ -0,0 +1,9 @@
1
+ // This is a manifest file that'll be compiled into including all the files listed below.
2
+ // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
3
+ // be included in the compiled file accessible from http://example.com/assets/application.js
4
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
5
+ // the compiled file.
6
+ //
7
+ //= require jquery
8
+ //= require jquery_ujs
9
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,56 @@
1
+ body { background-color: #fff; color: #333; }
2
+
3
+ body, p, ol, ul, td {
4
+ font-family: verdana, arial, helvetica, sans-serif;
5
+ font-size: 13px;
6
+ line-height: 18px;
7
+ }
8
+
9
+ pre {
10
+ background-color: #eee;
11
+ padding: 10px;
12
+ font-size: 11px;
13
+ }
14
+
15
+ a { color: #000; }
16
+ a:visited { color: #666; }
17
+ a:hover { color: #fff; background-color:#000; }
18
+
19
+ div.field, div.actions {
20
+ margin-bottom: 10px;
21
+ }
22
+
23
+ #notice {
24
+ color: green;
25
+ }
26
+
27
+ .field_with_errors {
28
+ padding: 2px;
29
+ background-color: red;
30
+ display: table;
31
+ }
32
+
33
+ #error_explanation {
34
+ width: 450px;
35
+ border: 2px solid red;
36
+ padding: 7px;
37
+ padding-bottom: 0;
38
+ margin-bottom: 20px;
39
+ background-color: #f0f0f0;
40
+ }
41
+
42
+ #error_explanation h2 {
43
+ text-align: left;
44
+ font-weight: bold;
45
+ padding: 5px 5px 5px 15px;
46
+ font-size: 12px;
47
+ margin: -7px;
48
+ margin-bottom: 0px;
49
+ background-color: #c00;
50
+ color: #fff;
51
+ }
52
+
53
+ #error_explanation ul li {
54
+ font-size: 12px;
55
+ list-style: square;
56
+ }
@@ -0,0 +1,7 @@
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
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,4 @@
1
+ module Selections
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,45 @@
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
@@ -0,0 +1,4 @@
1
+ module Selections
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Selections
2
+ module SelectorsHelper
3
+ end
4
+ end
@@ -0,0 +1,83 @@
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
+
@@ -0,0 +1,41 @@
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 %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing selection</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @selection %> |
6
+ <%= link_to 'Back', selections_path %>
@@ -0,0 +1,33 @@
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 %>
@@ -0,0 +1,5 @@
1
+ <h1>New selection</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', selections_path %>
@@ -0,0 +1,35 @@
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 %>
@@ -0,0 +1 @@
1
+ #ApplicationController.helper 'selections/selectors'
@@ -0,0 +1,5 @@
1
+ Selections::Engine.routes.draw do
2
+ scope :selections do
3
+ resources :selections
4
+ end
5
+ end
@@ -0,0 +1,15 @@
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