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.
- data/.gitignore +18 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/{MIT-LICENSE → LICENSE.txt} +4 -2
- data/README.md +237 -0
- data/Rakefile +3 -29
- data/lib/selections.rb +22 -6
- data/lib/selections/belongs_to_selection.rb +20 -0
- data/lib/selections/form_builder_extensions.rb +86 -0
- data/lib/selections/selectable.rb +125 -0
- data/lib/selections/version.rb +1 -1
- data/selections.gemspec +38 -0
- data/spec/selections/selectable_spec.rb +220 -0
- data/spec/selections/selections_tag_spec.rb +203 -0
- data/spec/spec_helper.rb +30 -21
- metadata +99 -177
- data/README.rdoc +0 -3
- data/app/assets/javascripts/selections/application.js +0 -9
- data/app/assets/javascripts/selections/selections.js +0 -2
- data/app/assets/stylesheets/scaffold.css +0 -56
- data/app/assets/stylesheets/selections/application.css +0 -7
- data/app/assets/stylesheets/selections/selections.css +0 -4
- data/app/controllers/selections/application_controller.rb +0 -4
- data/app/controllers/selections/selections_controller.rb +0 -45
- data/app/helpers/selections/application_helper.rb +0 -4
- data/app/helpers/selections/selectors_helper.rb +0 -4
- data/app/models/selections/selection.rb +0 -83
- data/app/views/layouts/selections/application.html.erb +0 -1
- data/app/views/selections/selections/_form.html.erb +0 -41
- data/app/views/selections/selections/edit.html.erb +0 -6
- data/app/views/selections/selections/index.html.erb +0 -33
- data/app/views/selections/selections/new.html.erb +0 -5
- data/app/views/selections/selections/show.html.erb +0 -35
- data/config/initializers/selections.rb +0 -1
- data/config/routes.rb +0 -5
- data/db/migrate/20120114024459_create_selections_selections.rb +0 -15
- data/lib/selections/engine.rb +0 -5
- data/lib/selections/selection_select.rb +0 -24
- data/lib/tasks/selections_tasks.rake +0 -4
- data/spec/controllers/companies_controller_spec.rb +0 -10
- data/spec/controllers/selections/selections_controller_spec.rb +0 -165
- data/spec/dummy/Rakefile +0 -7
- data/spec/dummy/app/assets/javascripts/application.js +0 -9
- data/spec/dummy/app/assets/javascripts/companies.js +0 -2
- data/spec/dummy/app/assets/stylesheets/application.css +0 -7
- data/spec/dummy/app/assets/stylesheets/companies.css +0 -4
- data/spec/dummy/app/assets/stylesheets/scaffold.css +0 -56
- data/spec/dummy/app/controllers/application_controller.rb +0 -2
- data/spec/dummy/app/controllers/companies_controller.rb +0 -83
- data/spec/dummy/app/helpers/application_helper.rb +0 -2
- data/spec/dummy/app/helpers/companies_helper.rb +0 -2
- data/spec/dummy/app/models/company.rb +0 -7
- data/spec/dummy/app/views/companies/_form.html.erb +0 -3
- data/spec/dummy/app/views/companies/edit.html.erb +0 -4
- data/spec/dummy/app/views/companies/index.html.erb +0 -27
- data/spec/dummy/app/views/companies/new.html.erb +0 -5
- data/spec/dummy/app/views/companies/show.html.erb +0 -20
- data/spec/dummy/app/views/layouts/application.html.erb +0 -11
- data/spec/dummy/config.ru +0 -4
- data/spec/dummy/config/application.rb +0 -45
- data/spec/dummy/config/boot.rb +0 -10
- data/spec/dummy/config/database.yml +0 -25
- data/spec/dummy/config/environment.rb +0 -5
- data/spec/dummy/config/environments/development.rb +0 -30
- data/spec/dummy/config/environments/production.rb +0 -60
- data/spec/dummy/config/environments/test.rb +0 -42
- data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/dummy/config/initializers/inflections.rb +0 -10
- data/spec/dummy/config/initializers/mime_types.rb +0 -5
- data/spec/dummy/config/initializers/secret_token.rb +0 -7
- data/spec/dummy/config/initializers/session_store.rb +0 -8
- data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/spec/dummy/config/locales/en.yml +0 -5
- data/spec/dummy/config/routes.rb +0 -6
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20120114125226_create_selections_selections.rb +0 -15
- data/spec/dummy/db/migrate/20120115055119_create_companies.rb +0 -12
- data/spec/dummy/db/schema.rb +0 -38
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +0 -94
- data/spec/dummy/log/test.log +0 -26360
- data/spec/dummy/public/404.html +0 -26
- data/spec/dummy/public/422.html +0 -26
- data/spec/dummy/public/500.html +0 -26
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +0 -6
- data/spec/helpers/selections/selections_helper_spec.rb +0 -15
- data/spec/models/company_spec.rb +0 -7
- data/spec/models/selections/selection_spec.rb +0 -219
- data/spec/routing/selections/selections_routing_spec.rb +0 -35
- data/spec/support/controller_route_fix.rb +0 -33
- data/spec/support/factories/company_factory.rb +0 -5
- data/spec/support/factories/selection_factory.rb +0 -5
- data/spec/views/companies/edit.html.erb_spec.rb +0 -76
- data/spec/views/companies/new.html.erb_spec.rb +0 -78
- data/spec/views/selections/selections/edit.html.erb_spec.rb +0 -28
- data/spec/views/selections/selections/index.html.erb_spec.rb +0 -40
- data/spec/views/selections/selections/new.html.erb_spec.rb +0 -28
- data/spec/views/selections/selections/show.html.erb_spec.rb +0 -30
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
require 'acts_as_tree'
|
|
2
|
+
|
|
3
|
+
module Selections
|
|
4
|
+
module Selectable
|
|
5
|
+
|
|
6
|
+
module ModelMixin
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
HIDDEN_POSITION = 999888777 #:nodoc:
|
|
10
|
+
|
|
11
|
+
included do
|
|
12
|
+
# Setup any required model information for a selectable model.
|
|
13
|
+
acts_as_tree
|
|
14
|
+
|
|
15
|
+
validate :name, :existence => true
|
|
16
|
+
validates_presence_of :name
|
|
17
|
+
validates_uniqueness_of :name, :scope => :parent_id
|
|
18
|
+
validates_uniqueness_of :system_code, :scope => :archived_at
|
|
19
|
+
validates_format_of :system_code, :with => /^[a-z][a-zA-Z_0-9]*$/, :message => "can only contain alphanumeric characters and '_', not spaces"
|
|
20
|
+
|
|
21
|
+
before_validation :auto_gen_system_code, :on => :create
|
|
22
|
+
before_validation :disable_system_code_change, :on => :update
|
|
23
|
+
after_validation :check_defaults
|
|
24
|
+
|
|
25
|
+
default_scope :order => [:position_value, :name]
|
|
26
|
+
|
|
27
|
+
scope :filter_archived_except_selected, lambda { |selected_id| {:conditions => ["archived_at is ? or id = ?", nil, selected_id.to_i]} }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
module ClassMethods
|
|
31
|
+
# Dynamic Lookups
|
|
32
|
+
#
|
|
33
|
+
# Assuming there is a list with the following system_codes
|
|
34
|
+
#
|
|
35
|
+
# example of a nested list of system codes:
|
|
36
|
+
# * priority
|
|
37
|
+
# * - high
|
|
38
|
+
# * - medium
|
|
39
|
+
# * - low
|
|
40
|
+
#
|
|
41
|
+
# Selection.priority => return priority instance
|
|
42
|
+
#
|
|
43
|
+
# Selection.priorities => returns [high, medium, low] instances (children)
|
|
44
|
+
|
|
45
|
+
def method_missing lookup_code, *options
|
|
46
|
+
if (scope = where(:system_code => lookup_code.to_s)).exists?
|
|
47
|
+
scope.first
|
|
48
|
+
elsif (scope = where(:system_code => lookup_code.to_s.singularize)).exists?
|
|
49
|
+
scope.first.children
|
|
50
|
+
else
|
|
51
|
+
super
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def to_s #:nodoc:
|
|
58
|
+
name.to_s
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Returns boolean true if current node has no children
|
|
62
|
+
def leaf?
|
|
63
|
+
children.where(parent_id: self.id).empty?
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Returns childrens children from root or any point
|
|
67
|
+
def level_2
|
|
68
|
+
Selection.where(parent_id: child_ids)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def position=(value) #:nodoc:
|
|
72
|
+
self.position_value = value || HIDDEN_POSITION
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def disable_system_code_change #:nodoc:
|
|
76
|
+
errors.add(:system_code, "cannot be changed") if system_code_changed?
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def position #:nodoc:
|
|
80
|
+
position_value unless position_value == HIDDEN_POSITION
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def auto_gen_system_code #:nodoc:
|
|
84
|
+
unless system_code
|
|
85
|
+
self.system_code= name.to_s.underscore.split(" ").join("_").singularize.underscore.gsub(/\W/, "_")
|
|
86
|
+
self.system_code= parent.system_code + "_" + self.system_code if parent
|
|
87
|
+
self.system_code.gsub!(/\_{2,}/, '_')
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def check_defaults #:nodoc:
|
|
92
|
+
siblings_with_default_set.update_attribute(:is_default, false) if self.parent && siblings_with_default_set && self.is_default
|
|
93
|
+
self.is_default = false if archived
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def siblings_with_default_set #:nodoc:
|
|
97
|
+
self.parent.children.where(:is_default => true).where("id != ?", self.id.to_i).first
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def archived #:nodoc:
|
|
101
|
+
!!archived_at
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def archived=(archived_checkbox) #:nodoc:
|
|
105
|
+
if archived_checkbox == "1"
|
|
106
|
+
self.archived_at = Time.now unless archived_at
|
|
107
|
+
else
|
|
108
|
+
self.archived_at = nil
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def sub_children
|
|
113
|
+
children.flat_map(&:children)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def selectable #:nodoc:
|
|
118
|
+
include ModelMixin
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
ActiveSupport.on_load :active_record do
|
|
122
|
+
extend Selections::Selectable
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
data/lib/selections/version.rb
CHANGED
data/selections.gemspec
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'selections/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |gem|
|
|
7
|
+
gem.name = "selections"
|
|
8
|
+
gem.version = Selections::VERSION
|
|
9
|
+
gem.authors = ["Nigel Rausch"]
|
|
10
|
+
gem.email = ["nigelr@brisbanerails.com"]
|
|
11
|
+
gem.description = %q{Selection list management and form and view helpers.
|
|
12
|
+
Manages one table to hold all selections items/dropdown lists ( tree ).
|
|
13
|
+
Dynamic lookup to find parent or children ( eg. Selection.priorities ).
|
|
14
|
+
Form helper to display lists ( eg. f.selections :priorities ).
|
|
15
|
+
Handling of archived items ( displaying if selected only ).
|
|
16
|
+
}
|
|
17
|
+
gem.summary = %q{Selection list management and form and view helpers.
|
|
18
|
+
Manages one table to hold all selections items/dropdown lists ( tree ).
|
|
19
|
+
Dynamic lookup to find parent or children ( eg. Selection.priorities ).
|
|
20
|
+
Form helper to display lists ( eg. f.selections :priorities ).
|
|
21
|
+
Handling of archived items ( displaying if selected only ).
|
|
22
|
+
}
|
|
23
|
+
gem.homepage = "https://github.com/nigelr/selections"
|
|
24
|
+
gem.files = `git ls-files`.split($/)
|
|
25
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
26
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
27
|
+
gem.require_paths = ["lib"]
|
|
28
|
+
|
|
29
|
+
gem.add_dependency 'activerecord', '~> 3.0'
|
|
30
|
+
gem.add_dependency 'actionpack', '~> 3.0'
|
|
31
|
+
gem.add_dependency 'acts_as_tree', '~> 1.2.0'
|
|
32
|
+
|
|
33
|
+
gem.add_development_dependency 'rake'
|
|
34
|
+
gem.add_development_dependency 'rspec'
|
|
35
|
+
gem.add_development_dependency 'sqlite3'
|
|
36
|
+
gem.add_development_dependency 'nokogiri'
|
|
37
|
+
|
|
38
|
+
end
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe Selections do
|
|
4
|
+
let(:parent) { Selection.create(name: "priority") }
|
|
5
|
+
let(:selection_1) { Selection.create(name: "low", parent_id: parent.id) }
|
|
6
|
+
let(:selection_2) { Selection.create(name: "medium", parent_id: parent.id) }
|
|
7
|
+
let(:selection_3) { Selection.create(name: "high", parent_id: parent.id) }
|
|
8
|
+
|
|
9
|
+
describe ".filter_archived_except_selected" do
|
|
10
|
+
before do
|
|
11
|
+
selection_1
|
|
12
|
+
selection_2
|
|
13
|
+
end
|
|
14
|
+
it "returns all when none selected" do
|
|
15
|
+
parent.children.filter_archived_except_selected(nil)
|
|
16
|
+
expect(parent.children.filter_archived_except_selected(nil)).to match_array([selection_2, selection_1])
|
|
17
|
+
end
|
|
18
|
+
it "returns all even when selected" do
|
|
19
|
+
expect(parent.children.filter_archived_except_selected(selection_2.id)).to match_array([selection_2, selection_1])
|
|
20
|
+
end
|
|
21
|
+
context "archived" do
|
|
22
|
+
before :each do
|
|
23
|
+
selection_2.update_attribute(:archived, "1")
|
|
24
|
+
end
|
|
25
|
+
it "returns unarchived items" do
|
|
26
|
+
expect(parent.children.filter_archived_except_selected(nil)).to match_array([selection_1])
|
|
27
|
+
end
|
|
28
|
+
it "returns only unarchived items when archived is not selected" do
|
|
29
|
+
expect(parent.children.filter_archived_except_selected(selection_1.id)).to match_array([selection_1])
|
|
30
|
+
end
|
|
31
|
+
it "returns archived item if selected" do
|
|
32
|
+
expect(parent.children.filter_archived_except_selected(selection_2.id)).to match_array([selection_2, selection_1])
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
describe ".to_s" do
|
|
38
|
+
it "returns name" do
|
|
39
|
+
expect(selection_1.to_s).to eq(selection_1.name)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
describe ".leaf?" do
|
|
45
|
+
it { selection_1; expect(parent.leaf?).to be_false }
|
|
46
|
+
it { expect(selection_1.leaf?).to be_true }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
describe ".sub_children" do
|
|
50
|
+
before do
|
|
51
|
+
selection_1.children = [Selection.create(name: "sub_1"), Selection.create(name: "sub_2")]
|
|
52
|
+
selection_2.children = [Selection.create(name: "sub_3")]
|
|
53
|
+
end
|
|
54
|
+
it "gets all the childrens children" do
|
|
55
|
+
expect(parent.sub_children).to match_array(selection_1.children + selection_2.children)
|
|
56
|
+
end
|
|
57
|
+
it "return empty array from leaf" do
|
|
58
|
+
expect(selection_1.children.first.sub_children).to eq([])
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
describe ".position=" do
|
|
63
|
+
it "sets to HIDDEN_POSITION if blank" do
|
|
64
|
+
expect(Selection.create(name: "position", position: nil).position_value).to eq(Selection::HIDDEN_POSITION)
|
|
65
|
+
end
|
|
66
|
+
it "sets to position_value to 45" do
|
|
67
|
+
expect(Selection.create(name: "position", position: 45).position_value).to eq(45)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
describe ".position" do
|
|
72
|
+
it "should display position if not HIDDEN_POSITION" do
|
|
73
|
+
expect(Selection.create(name: "position", position: 46).position).to eq(46)
|
|
74
|
+
end
|
|
75
|
+
it "should not display position if HIDDEN_POSITION" do
|
|
76
|
+
expect(Selection.create(name: "position", position: nil).position).to be_nil
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
describe ".auto_gen_system_code" do
|
|
81
|
+
it "only generates if blank" do
|
|
82
|
+
expect(Selection.create(name: "position", system_code: "hello_world").system_code).to eq("hello_world")
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
context "chaining of system_codes" do
|
|
86
|
+
before do
|
|
87
|
+
@parent = Selection.create(name: "Board Chairman")
|
|
88
|
+
end
|
|
89
|
+
it "create system_code from name" do
|
|
90
|
+
expect(@parent.system_code).to eq("board_chairman")
|
|
91
|
+
end
|
|
92
|
+
context "child" do
|
|
93
|
+
before do
|
|
94
|
+
@child = Selection.create(name: "CEO", parent: @parent)
|
|
95
|
+
end
|
|
96
|
+
it "create system_code from name" do
|
|
97
|
+
expect(@child.system_code).to eq("board_chairman_ceo")
|
|
98
|
+
end
|
|
99
|
+
context "child" do
|
|
100
|
+
it "create system_code from name" do
|
|
101
|
+
expect(Selection.create(name: "head janitor", parent: @child).system_code).to eq("board_chairman_ceo_head_janitor")
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
describe ".siblings_with_default_set" do
|
|
109
|
+
before do
|
|
110
|
+
selection_1
|
|
111
|
+
selection_2
|
|
112
|
+
selection_3
|
|
113
|
+
end
|
|
114
|
+
it "returns none when no default set" do
|
|
115
|
+
expect(selection_2.siblings_with_default_set).to eq(nil)
|
|
116
|
+
end
|
|
117
|
+
context "default set" do
|
|
118
|
+
before do
|
|
119
|
+
selection_2.update_attribute(:is_default, true)
|
|
120
|
+
end
|
|
121
|
+
it "returns item with default set" do
|
|
122
|
+
expect(selection_1.siblings_with_default_set).to eq(selection_2)
|
|
123
|
+
end
|
|
124
|
+
it "does not returns item with default set when self" do
|
|
125
|
+
expect(selection_2.siblings_with_default_set).to eq(nil)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
describe ".check_defaults" do
|
|
131
|
+
before do
|
|
132
|
+
selection_1
|
|
133
|
+
selection_2
|
|
134
|
+
selection_3
|
|
135
|
+
end
|
|
136
|
+
it("selection_1 should not be set") { expect(selection_1.reload.is_default).to be_false }
|
|
137
|
+
it("selection_2 should not be set") { expect(selection_2.reload.is_default).to be_false }
|
|
138
|
+
it("selection_3 should not be set") { expect(selection_3.reload.is_default).to be_false }
|
|
139
|
+
|
|
140
|
+
context "setting one item to default" do
|
|
141
|
+
before { selection_2.update_attributes(is_default: true) }
|
|
142
|
+
it("should set self as default") { expect(selection_2.reload.is_default).to be_true }
|
|
143
|
+
it("selection_1 should not be set") { expect(selection_1.reload.is_default).to be_false }
|
|
144
|
+
it("selection_3 should not be set") { expect(selection_3.reload.is_default).to be_false }
|
|
145
|
+
context "setting same item to default" do
|
|
146
|
+
before { selection_2.update_attributes(is_default: true) }
|
|
147
|
+
it("should set self as default") { expect(selection_2.reload.is_default).to be_true }
|
|
148
|
+
it("selection_1 should not be set") { expect(selection_1.reload.is_default).to be_false }
|
|
149
|
+
it("selection_3 should not be set") { expect(selection_3.reload.is_default).to be_false }
|
|
150
|
+
end
|
|
151
|
+
context "setting other item to default" do
|
|
152
|
+
before { selection_1.update_attributes(is_default: true) }
|
|
153
|
+
it("should set self as default") { expect(selection_1.reload.is_default).to be_true }
|
|
154
|
+
it("selection_2 should not be set") { expect(selection_2.reload.is_default).to be_false }
|
|
155
|
+
it("selection_3 should not be set") { expect(selection_3.reload.is_default).to be_false }
|
|
156
|
+
end
|
|
157
|
+
context "removing default setting" do
|
|
158
|
+
before { selection_2.update_attributes(is_default: false) }
|
|
159
|
+
it("should unset self as default") { expect(selection_1.reload.is_default).to be_false }
|
|
160
|
+
it("selection_2 should not be set") { expect(selection_2.reload.is_default).to be_false }
|
|
161
|
+
it("selection_3 should not be set") { expect(selection_3.reload.is_default).to be_false }
|
|
162
|
+
end
|
|
163
|
+
context "archive item with default" do
|
|
164
|
+
before { selection_2.update_attributes(archived: "1") }
|
|
165
|
+
it("should unset self as default") { expect(selection_2.reload.is_default).to be_false }
|
|
166
|
+
it("selection_1 should not be set") { expect(selection_1.reload.is_default).to be_false }
|
|
167
|
+
it("selection_3 should not be set") { expect(selection_3.reload.is_default).to be_false }
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
describe ".archived=" do
|
|
173
|
+
before do
|
|
174
|
+
@stubbed_time = Time.parse("01/01/2010 10:00")
|
|
175
|
+
Time.stub(:now).and_return(@stubbed_time)
|
|
176
|
+
selection_1.update_attributes(archived: "1")
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
it "should set archived" do
|
|
180
|
+
expect(selection_1.archived_at).to eq @stubbed_time
|
|
181
|
+
end
|
|
182
|
+
it "remain archived and not change date when set again" do
|
|
183
|
+
Time.stub(:now).and_return(Time.parse("12/12/2012 10:00"))
|
|
184
|
+
selection_1.update_attributes(archived: "1")
|
|
185
|
+
expect(selection_1.archived_at).to eq @stubbed_time
|
|
186
|
+
end
|
|
187
|
+
it "un-archives item" do
|
|
188
|
+
selection_1.update_attributes(archived: "0")
|
|
189
|
+
expect(selection_1.archived_at).to be_nil
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
describe ".archived" do
|
|
194
|
+
it "when archived set" do
|
|
195
|
+
selection_1.update_attributes(archived: "1")
|
|
196
|
+
expect(selection_1.archived).to be_true
|
|
197
|
+
end
|
|
198
|
+
it "when archived set" do
|
|
199
|
+
selection_1.update_attributes(archived: "0")
|
|
200
|
+
expect(selection_1.archived).to be_false
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
context "selecting" do
|
|
205
|
+
before do
|
|
206
|
+
selection_1
|
|
207
|
+
selection_2
|
|
208
|
+
end
|
|
209
|
+
it "singular" do
|
|
210
|
+
expect(Selection.send(parent.system_code)).to eq(parent)
|
|
211
|
+
end
|
|
212
|
+
it "plural" do
|
|
213
|
+
expect(Selection.send(parent.system_code.pluralize)).to eq(parent.children)
|
|
214
|
+
end
|
|
215
|
+
#TODO this needs to ne an option
|
|
216
|
+
#it "should not error when non existent" do
|
|
217
|
+
# expect(Selection.non_existent_system_code).to eq([])
|
|
218
|
+
#end
|
|
219
|
+
end
|
|
220
|
+
end
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
include Selections::FormBuilderExtensions
|
|
5
|
+
describe SelectionTag do
|
|
6
|
+
let(:parent) { Selection.create(name: "priority") }
|
|
7
|
+
let(:model_parent) { Selection.create(name: "ticket_priority") }
|
|
8
|
+
let(:selection_1) { Selection.create(name: "low", parent_id: parent.id) }
|
|
9
|
+
let(:selection_2) { Selection.create(name: "medium", parent_id: parent.id) }
|
|
10
|
+
let(:selection_3) { Selection.create(name: "high", parent_id: parent.id) }
|
|
11
|
+
let(:form) { ActionView::Helpers::FormBuilder.new(:ticket, ticket, ActionView::Base.new, {}, Proc.new {}) }
|
|
12
|
+
let(:ticket) { Ticket.create(:name => "railscamp") }
|
|
13
|
+
let(:all_selections) do
|
|
14
|
+
parent
|
|
15
|
+
selection_1
|
|
16
|
+
selection_2
|
|
17
|
+
selection_3
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
describe ".system_code" do
|
|
22
|
+
context "direct parent" do
|
|
23
|
+
before { parent }
|
|
24
|
+
it "returns priority selection item" do
|
|
25
|
+
expect(SelectionTag.new(form, ticket, :priority, {}, {}).system_code).to eq(parent)
|
|
26
|
+
end
|
|
27
|
+
it "does not find" do
|
|
28
|
+
expect(SelectionTag.new(form, ticket, :non_existent, {}, {}).system_code).to be_nil
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "finds with form model prefixed" do
|
|
33
|
+
model_parent
|
|
34
|
+
expect(SelectionTag.new(form, ticket, :priority, {}, {}).system_code).to eq(model_parent)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
describe ".items" do
|
|
39
|
+
before { all_selections }
|
|
40
|
+
|
|
41
|
+
it "returns all children items" do
|
|
42
|
+
expect(SelectionTag.new(form, ticket, :priority, {}, {}).items.all).to eq(parent.children)
|
|
43
|
+
end
|
|
44
|
+
context "archived" do
|
|
45
|
+
before { selection_2.update_attribute(:archived, "1") }
|
|
46
|
+
it "returns only non archived items" do
|
|
47
|
+
expect(SelectionTag.new(form, ticket, :priority, {}, {}).items).to eq(parent.children - [selection_2])
|
|
48
|
+
end
|
|
49
|
+
it "returns archived items when selected" do
|
|
50
|
+
ticket.update_attribute(:priority_id, selection_2.id)
|
|
51
|
+
expect(SelectionTag.new(form, ticket, :priority, {}, {}).items.all).to eq(parent.children)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
describe ".field_id" do
|
|
57
|
+
it("when string") { expect(SelectionTag.new(nil, nil, "priority", {}, {}).field_id).to eq(:priority_id) }
|
|
58
|
+
it("when symbol") { expect(SelectionTag.new(nil, nil, :priority, {}, {}).field_id).to eq(:priority_id) }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
describe ".system_code_name" do
|
|
62
|
+
it("sets to field name") { expect(SelectionTag.new(nil, nil, :priority, {}, {}).system_code_name).to eq(:priority) }
|
|
63
|
+
it("when override system_code") { expect(SelectionTag.new(nil, nil, :hello, {}, {system_code: :priority}).system_code_name).to eq(:priority) }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
context "include blank" do
|
|
67
|
+
before { all_selections }
|
|
68
|
+
|
|
69
|
+
context "when not set" do
|
|
70
|
+
context "new form" do
|
|
71
|
+
it("has blank") { expect(SelectionTag.new(form, Ticket.new, :priority, {}, {}).include_blank?).to be_true }
|
|
72
|
+
it "has no blank when default set" do
|
|
73
|
+
selection_1.update_attribute(:is_default, true)
|
|
74
|
+
expect(SelectionTag.new(form, Ticket.new, :priority, {}, {}).include_blank?).to be_false
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
context "edit form" do
|
|
78
|
+
it("has no blank when ticket.priority_id is set") do
|
|
79
|
+
ticket.update_attribute(:priority_id, selection_3.id)
|
|
80
|
+
expect(SelectionTag.new(form, ticket, :priority, {}, {}).include_blank?).to be_false
|
|
81
|
+
end
|
|
82
|
+
it("has blank when ticket.priority_id is nil") { expect(SelectionTag.new(form, ticket, :priority, {}, {}).include_blank?).to be_true }
|
|
83
|
+
it "has no blank when ticket.priority_id is nil and default set" do
|
|
84
|
+
selection_1.update_attribute(:is_default, true)
|
|
85
|
+
expect(SelectionTag.new(form, ticket, :priority, {}, {}).include_blank?).to be_false
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
context "when set false" do
|
|
91
|
+
context "new form" do
|
|
92
|
+
it("has no blank") { expect(SelectionTag.new(form, Ticket.new, :priority, {}, {include_blank: false}).include_blank?).to be_false }
|
|
93
|
+
it "has no blank when default set" do
|
|
94
|
+
selection_1.update_attribute(:is_default, true)
|
|
95
|
+
expect(SelectionTag.new(form, Ticket.new, :priority, {}, {include_blank: false}).include_blank?).to be_false
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
context "edit form" do
|
|
99
|
+
it("has no blank when ticket.priority_id is set") do
|
|
100
|
+
ticket.update_attribute(:priority_id, selection_3.id)
|
|
101
|
+
expect(SelectionTag.new(form, ticket, :priority, {}, {include_blank: false}).include_blank?).to be_false
|
|
102
|
+
end
|
|
103
|
+
it("has no blank even when ticket.priority_id is nil") { expect(SelectionTag.new(form, ticket, :priority, {}, {include_blank: false}).include_blank?).to be_false }
|
|
104
|
+
it "has no blank when ticket.priority_id is nil and default set" do
|
|
105
|
+
selection_1.update_attribute(:is_default, true)
|
|
106
|
+
expect(SelectionTag.new(form, ticket, :priority, {}, {include_blank: false}).include_blank?).to be_false
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
context "when set to true" do
|
|
112
|
+
context "new form" do
|
|
113
|
+
it("has blank") { expect(SelectionTag.new(form, Ticket.new, :priority, {}, {include_blank: true}).include_blank?).to be_true }
|
|
114
|
+
it "has blank even when default set" do
|
|
115
|
+
selection_1.update_attribute(:is_default, true)
|
|
116
|
+
expect(SelectionTag.new(form, Ticket.new, :priority, {}, {include_blank: true}).include_blank?).to be_true
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
context "edit form" do
|
|
120
|
+
it("has blank even when ticket.priority_id is set") do
|
|
121
|
+
ticket.update_attribute(:priority_id, selection_3.id)
|
|
122
|
+
expect(SelectionTag.new(form, ticket, :priority, {}, {include_blank: true}).include_blank?).to be_true
|
|
123
|
+
end
|
|
124
|
+
it("has blank even when ticket.priority_id is nil") { expect(SelectionTag.new(form, ticket, :priority, {}, {include_blank: true}).include_blank?).to be_true }
|
|
125
|
+
it "has blank even when ticket.priority_id is nil and default set" do
|
|
126
|
+
selection_1.update_attribute(:is_default, true)
|
|
127
|
+
expect(SelectionTag.new(form, ticket, :priority, {}, {include_blank: true}).include_blank?).to be_true
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
context ".default_item" do
|
|
134
|
+
before { all_selections }
|
|
135
|
+
it "returns nil when no default set" do
|
|
136
|
+
expect(SelectionTag.new(form, Ticket.new, :priority, {}, {include_blank: false}).default_item).to be_blank
|
|
137
|
+
end
|
|
138
|
+
it "should set to default item" do
|
|
139
|
+
selection_2.update_attribute(:is_default, true)
|
|
140
|
+
expect(SelectionTag.new(form, Ticket.new, :priority, {}, {include_blank: false}).default_item).to eq(selection_2.id.to_s)
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
context ".selected_item" do
|
|
145
|
+
before { all_selections }
|
|
146
|
+
|
|
147
|
+
context "when default not set" do
|
|
148
|
+
it("when new form") { expect(SelectionTag.new(form, Ticket.new, :priority, {}, {}).selected_item).to eq("") }
|
|
149
|
+
it "when edit form with ticket.priority_id set" do
|
|
150
|
+
ticket.update_attribute(:priority_id, selection_3.id)
|
|
151
|
+
expect(SelectionTag.new(form, ticket, :priority, {}, {}).selected_item).to eq(selection_3.id.to_s)
|
|
152
|
+
end
|
|
153
|
+
it("when edit form with no ticket.priority_id set") { expect(SelectionTag.new(form, ticket, :priority, {}, {}).selected_item).to eq("") }
|
|
154
|
+
end
|
|
155
|
+
context "when default is set" do
|
|
156
|
+
before { selection_2.update_attribute(:is_default, true) }
|
|
157
|
+
it("when new form") { expect(SelectionTag.new(form, Ticket.new, :priority, {}, {}).selected_item).to eq(selection_2.id.to_s) }
|
|
158
|
+
it "when edit form with ticket.priority_id set" do
|
|
159
|
+
ticket.update_attribute(:priority_id, selection_3.id)
|
|
160
|
+
expect(SelectionTag.new(form, ticket, :priority, {}, {}).selected_item).to eq(selection_3.id.to_s)
|
|
161
|
+
end
|
|
162
|
+
it("when edit form with no ticket.priority_id set") { expect(SelectionTag.new(form, ticket, :priority, {}, {}).selected_item).to eq("") }
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
describe ".to_tag" do
|
|
167
|
+
it "displays warning when system_code does not exist" do
|
|
168
|
+
expect(SelectionTag.new(form, ticket, :priority, {}, {}).to_tag).to eq("Invalid system_code of 'priority'")
|
|
169
|
+
end
|
|
170
|
+
context "valid system_code" do
|
|
171
|
+
before { all_selections }
|
|
172
|
+
|
|
173
|
+
context "new form" do
|
|
174
|
+
context "no default" do
|
|
175
|
+
it("has no selected item") { expect(Nokogiri::HTML(SelectionTag.new(form, Ticket.new, :priority, {}, {}).to_tag).search("option[selected]")).to be_empty }
|
|
176
|
+
it("has a blank option") { expect(Nokogiri::HTML(SelectionTag.new(form, Ticket.new, :priority, {}, {}).to_tag).search("option[value='']").count).to eq(1) }
|
|
177
|
+
end
|
|
178
|
+
context "default is set" do
|
|
179
|
+
before { selection_3.update_attribute(:is_default, true) }
|
|
180
|
+
|
|
181
|
+
it("has selection_3 selected") { expect(Nokogiri::HTML(SelectionTag.new(form, Ticket.new, :priority, {}, {}).to_tag).search("option[selected]").first.content).to eq(selection_3.name) }
|
|
182
|
+
it("has no blank option") { expect(Nokogiri::HTML(SelectionTag.new(form, Ticket.new, :priority, {}, {}).to_tag).search("option[value='']").count).to eq(0) }
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
context "edit form" do
|
|
187
|
+
context "relation (priority_id) is nil" do
|
|
188
|
+
it("has no selected item") { expect(Nokogiri::HTML(SelectionTag.new(form, ticket, :priority, {}, {}).to_tag).search("option[selected]")).to be_empty }
|
|
189
|
+
it("has a blank option") { expect(Nokogiri::HTML(SelectionTag.new(form, ticket, :priority, {}, {}).to_tag).search("option[value='']").count).to eq(1) }
|
|
190
|
+
end
|
|
191
|
+
context "when relation (priority_id) is set to selection_3" do
|
|
192
|
+
before { ticket.update_attribute(:priority_id, selection_3.id) }
|
|
193
|
+
|
|
194
|
+
it("item is selected") { expect(Nokogiri::HTML(SelectionTag.new(form, ticket, :priority, {}, {}).to_tag).search("option[selected]").first.content).to eq(selection_3.name) }
|
|
195
|
+
it("has no blank option") { expect(Nokogiri::HTML(SelectionTag.new(form, ticket, :priority, {}, {}).to_tag).search("option[value='']").count).to eq(0) }
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
it "returns valid html" do
|
|
199
|
+
expect(SelectionTag.new(form, ticket, :priority, {}, {}).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
|
+
end
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
end
|