custom_fields 0.0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +18 -0
- data/init.rb +2 -0
- data/lib/custom_fields.rb +28 -0
- data/lib/custom_fields/custom_fields_for.rb +50 -0
- data/lib/custom_fields/extensions/mongoid/associations/embeds_many.rb +31 -0
- data/lib/custom_fields/extensions/mongoid/associations/proxy.rb +20 -0
- data/lib/custom_fields/extensions/mongoid/associations/references_many.rb +33 -0
- data/lib/custom_fields/extensions/mongoid/hierarchy.rb +28 -0
- data/lib/custom_fields/field.rb +82 -0
- data/lib/custom_fields/proxy_class_enabler.rb +40 -0
- data/lib/custom_fields/types/boolean.rb +29 -0
- data/lib/custom_fields/types/category.rb +88 -0
- data/lib/custom_fields/types/date.rb +35 -0
- data/lib/custom_fields/types/default.rb +37 -0
- data/lib/custom_fields/types/file.rb +27 -0
- data/lib/custom_fields/types/string.rb +13 -0
- data/lib/custom_fields/types/text.rb +15 -0
- data/spec/integration/custom_fields_for_spec.rb +28 -0
- data/spec/integration/types/category_spec.rb +48 -0
- data/spec/integration/types/file_spec.rb +18 -0
- data/spec/models/person.rb +10 -0
- data/spec/models/project.rb +18 -0
- data/spec/models/task.rb +10 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/support/carrierwave.rb +31 -0
- data/spec/support/mongoid.rb +6 -0
- data/spec/unit/custom_field_spec.rb +42 -0
- data/spec/unit/custom_fields_for_spec.rb +106 -0
- data/spec/unit/proxy_class_enabler_spec.rb +25 -0
- data/spec/unit/types/boolean_spec.rb +81 -0
- data/spec/unit/types/category_spec.rb +116 -0
- data/spec/unit/types/date_spec.rb +59 -0
- data/spec/unit/types/file_spec.rb +23 -0
- metadata +116 -0
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CustomFields::ProxyClassEnabler do
|
4
|
+
|
5
|
+
context '#proxy klass' do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@klass = Task.to_klass_with_custom_fields([])
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'does not be flagged as a inherited document' do
|
12
|
+
@klass.new.hereditary?.should be_false
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'has a list of custom fields' do
|
16
|
+
@klass.custom_fields.should == []
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'has the exact same model name than its parent' do
|
20
|
+
@klass.model_name.should == 'Task'
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CustomFields::Types::Boolean do
|
4
|
+
|
5
|
+
context 'on field class' do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@field = CustomFields::Field.new
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'returns true if it is a Boolean' do
|
12
|
+
@field.kind = 'boolean'
|
13
|
+
@field.boolean?.should be_true
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'returns false if it is not a Boolean' do
|
17
|
+
@field.kind = 'string'
|
18
|
+
@field.boolean?.should be_false
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'on target class' do
|
24
|
+
|
25
|
+
before(:each) do
|
26
|
+
@project = build_project_with_boolean
|
27
|
+
end
|
28
|
+
|
29
|
+
context '#setting' do
|
30
|
+
|
31
|
+
context '#true' do
|
32
|
+
|
33
|
+
it 'sets value from an integer' do
|
34
|
+
@project.active = 1
|
35
|
+
@project.active.should == true
|
36
|
+
@project.field_1.should == '1'
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'sets value from a string' do
|
40
|
+
@project.active = '1'
|
41
|
+
@project.active.should == true
|
42
|
+
@project.field_1.should == '1'
|
43
|
+
|
44
|
+
@project.active = 'true'
|
45
|
+
@project.active.should == true
|
46
|
+
@project.field_1.should == 'true'
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
context '#false' do
|
52
|
+
|
53
|
+
it 'sets value from an integer' do
|
54
|
+
@project.active = 0
|
55
|
+
@project.active.should == false
|
56
|
+
@project.field_1.should == '0'
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'sets value from a string' do
|
60
|
+
@project.active = '0'
|
61
|
+
@project.active.should == false
|
62
|
+
@project.field_1.should == '0'
|
63
|
+
|
64
|
+
@project.active = 'false'
|
65
|
+
@project.active.should == false
|
66
|
+
@project.field_1.should == 'false'
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
def build_project_with_boolean
|
76
|
+
field = CustomFields::Field.new(:label => 'Active', :_name => 'field_1', :kind => 'Boolean')
|
77
|
+
field.stubs(:valid?).returns(true)
|
78
|
+
Project.to_klass_with_custom_fields(field).new
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CustomFields::Types::Category do
|
4
|
+
|
5
|
+
context 'on field class' do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@field = CustomFields::Field.new
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'has the category items field' do
|
12
|
+
@field.respond_to?(:category_items).should be_true
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'has the apply method used for the target object' do
|
16
|
+
@field.respond_to?(:apply_category_type).should be_true
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'returns true if it is a Category' do
|
20
|
+
@field.kind = 'category'
|
21
|
+
@field.category?.should be_true
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'returns false if it is not a Category' do
|
25
|
+
@field.kind = 'string'
|
26
|
+
@field.category?.should be_false
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'on target class' do
|
32
|
+
|
33
|
+
before(:each) do
|
34
|
+
@project = build_project_with_category
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'has getter/setter' do
|
38
|
+
@project.respond_to?(:global_category).should be_true
|
39
|
+
@project.respond_to?(:global_category=).should be_true
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'has the values of this category' do
|
43
|
+
@project.class.global_category_names.should == %w{Maintenance Design Development}
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'sets the category from a name' do
|
47
|
+
@project.global_category = 'Design'
|
48
|
+
@project.global_category.should == 'Design'
|
49
|
+
@project.field_1.should == fake_bson_id(42)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'does not set the category if it does not exit' do
|
53
|
+
@project.global_category = 'Accounting'
|
54
|
+
@project.global_category.should be_nil
|
55
|
+
@project.field_1.should be_nil
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'group by category' do
|
59
|
+
|
60
|
+
before(:each) do
|
61
|
+
seed_projects
|
62
|
+
@groups = @project.class.group_by_global_category
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'is an non empty array' do
|
66
|
+
@groups.class.should == Array
|
67
|
+
@groups.size.should == 3
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'is an array of hash composed of a name' do
|
71
|
+
@groups.collect { |g| g[:name] }.should == %w{Maintenance Design Development}
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'is an array of hash composed of a list of objects' do
|
75
|
+
@groups[0][:items].size.should == 0
|
76
|
+
@groups[1][:items].size.should == 1
|
77
|
+
@groups[2][:items].size.should == 2
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'passes method to retrieve items' do
|
81
|
+
@project.class.expects(:ordered)
|
82
|
+
@project.class.group_by_global_category(:ordered)
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
def build_project_with_category
|
90
|
+
field = build_category
|
91
|
+
Project.to_klass_with_custom_fields(field).new
|
92
|
+
end
|
93
|
+
|
94
|
+
def build_category
|
95
|
+
field = CustomFields::Field.new(:label => 'global_category', :_name => 'field_1', :kind => 'Category')
|
96
|
+
field.stubs(:valid?).returns(true)
|
97
|
+
field.category_items.build :name => 'Development', :_id => fake_bson_id(41), :position => 2
|
98
|
+
field.category_items.build :name => 'Design', :_id => fake_bson_id(42), :position => 1
|
99
|
+
field.category_items.build :name => 'Maintenance', :_id => fake_bson_id(43), :position => 0
|
100
|
+
field
|
101
|
+
end
|
102
|
+
|
103
|
+
def seed_projects
|
104
|
+
list = [
|
105
|
+
@project.class.new(:name => 'Locomotive CMS', :global_category => fake_bson_id(41)),
|
106
|
+
@project.class.new(:name => 'Ruby on Rails', :global_category => fake_bson_id(41)),
|
107
|
+
@project.class.new(:name => 'Dribble', :global_category => fake_bson_id(42))
|
108
|
+
]
|
109
|
+
@project.class.stubs(:all).returns(list)
|
110
|
+
end
|
111
|
+
|
112
|
+
def fake_bson_id(id)
|
113
|
+
BSON::ObjectID(id.to_s.rjust(24, '0'))
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CustomFields::Types::Date do
|
4
|
+
|
5
|
+
context 'on field class' do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@field = CustomFields::Field.new
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'returns true if it is a Date' do
|
12
|
+
@field.kind = 'Date'
|
13
|
+
@field.date?.should be_true
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'returns false if it is not a Date' do
|
17
|
+
@field.kind = 'string'
|
18
|
+
@field.date?.should be_false
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'on target class' do
|
24
|
+
|
25
|
+
before(:each) do
|
26
|
+
@project = build_project_with_date
|
27
|
+
@date = Date.parse('2010-06-29')
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'sets value from a date' do
|
31
|
+
@project.started_at = @date
|
32
|
+
@project.started_at.should == '2010-06-29'
|
33
|
+
@project.field_1.class.should == Date
|
34
|
+
@project.field_1.should == @date
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'sets value from a string' do
|
38
|
+
@project.started_at = '2010-06-29'
|
39
|
+
@project.started_at.class.should == String
|
40
|
+
@project.started_at.should == '2010-06-29'
|
41
|
+
@project.field_1.class.should == Date
|
42
|
+
@project.field_1.should == @date
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'sets nil value' do
|
46
|
+
@project.started_at = nil
|
47
|
+
@project.started_at.should be_nil
|
48
|
+
@project.field_1.should be_nil
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
def build_project_with_date
|
54
|
+
field = CustomFields::Field.new(:label => 'Started at', :_name => 'field_1', :kind => 'Date')
|
55
|
+
field.stubs(:valid?).returns(true)
|
56
|
+
Project.to_klass_with_custom_fields(field).new
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CustomFields::Types::Date do
|
4
|
+
|
5
|
+
context 'on field class' do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@field = CustomFields::Field.new
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'returns true if it is a Date' do
|
12
|
+
@field.kind = 'File'
|
13
|
+
@field.file?.should be_true
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'returns false if it is not a Date' do
|
17
|
+
@field.kind = 'string'
|
18
|
+
@field.file?.should be_false
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: custom_fields
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 77
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
- 1
|
11
|
+
version: 0.0.0.1
|
12
|
+
platform: ruby
|
13
|
+
authors:
|
14
|
+
- Didier Lafforgue
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-08-22 00:00:00 +02:00
|
20
|
+
default_executable:
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: Manage custom fields to a mongoid document or a collection. This module is one of the core features we implemented in our custom cms named Locomotive.
|
24
|
+
email:
|
25
|
+
- didier@nocoffee.fr
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README
|
32
|
+
files:
|
33
|
+
- init.rb
|
34
|
+
- lib/custom_fields.rb
|
35
|
+
- lib/custom_fields/custom_fields_for.rb
|
36
|
+
- lib/custom_fields/extensions/mongoid/associations/embeds_many.rb
|
37
|
+
- lib/custom_fields/extensions/mongoid/associations/proxy.rb
|
38
|
+
- lib/custom_fields/extensions/mongoid/associations/references_many.rb
|
39
|
+
- lib/custom_fields/extensions/mongoid/hierarchy.rb
|
40
|
+
- lib/custom_fields/field.rb
|
41
|
+
- lib/custom_fields/proxy_class_enabler.rb
|
42
|
+
- lib/custom_fields/types/boolean.rb
|
43
|
+
- lib/custom_fields/types/category.rb
|
44
|
+
- lib/custom_fields/types/date.rb
|
45
|
+
- lib/custom_fields/types/default.rb
|
46
|
+
- lib/custom_fields/types/file.rb
|
47
|
+
- lib/custom_fields/types/string.rb
|
48
|
+
- lib/custom_fields/types/text.rb
|
49
|
+
- README
|
50
|
+
- spec/integration/custom_fields_for_spec.rb
|
51
|
+
- spec/integration/types/category_spec.rb
|
52
|
+
- spec/integration/types/file_spec.rb
|
53
|
+
- spec/models/person.rb
|
54
|
+
- spec/models/project.rb
|
55
|
+
- spec/models/task.rb
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
- spec/support/carrierwave.rb
|
58
|
+
- spec/support/mongoid.rb
|
59
|
+
- spec/unit/custom_field_spec.rb
|
60
|
+
- spec/unit/custom_fields_for_spec.rb
|
61
|
+
- spec/unit/proxy_class_enabler_spec.rb
|
62
|
+
- spec/unit/types/boolean_spec.rb
|
63
|
+
- spec/unit/types/category_spec.rb
|
64
|
+
- spec/unit/types/date_spec.rb
|
65
|
+
- spec/unit/types/file_spec.rb
|
66
|
+
has_rdoc: true
|
67
|
+
homepage: http://github.com/locomotivecms/custom_fields
|
68
|
+
licenses: []
|
69
|
+
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options:
|
72
|
+
- --charset=UTF-8
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
hash: 3
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
requirements: []
|
94
|
+
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 1.3.7
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: Custom fields plugin for Mongoid
|
100
|
+
test_files:
|
101
|
+
- spec/integration/custom_fields_for_spec.rb
|
102
|
+
- spec/integration/types/category_spec.rb
|
103
|
+
- spec/integration/types/file_spec.rb
|
104
|
+
- spec/models/person.rb
|
105
|
+
- spec/models/project.rb
|
106
|
+
- spec/models/task.rb
|
107
|
+
- spec/spec_helper.rb
|
108
|
+
- spec/support/carrierwave.rb
|
109
|
+
- spec/support/mongoid.rb
|
110
|
+
- spec/unit/custom_field_spec.rb
|
111
|
+
- spec/unit/custom_fields_for_spec.rb
|
112
|
+
- spec/unit/proxy_class_enabler_spec.rb
|
113
|
+
- spec/unit/types/boolean_spec.rb
|
114
|
+
- spec/unit/types/category_spec.rb
|
115
|
+
- spec/unit/types/date_spec.rb
|
116
|
+
- spec/unit/types/file_spec.rb
|