congo 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,128 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/functional_spec_helper')
2
+
3
+ describe 'Scoper' do
4
+
5
+ before(:each) do
6
+ Congo::ProxyScoper.destroy_all
7
+ Congo::ContentType.destroy_all
8
+ end
9
+
10
+ it 'should be used inside any AR models' do
11
+ website = Website.new(42, 'My simple website')
12
+ website.id.should == 42
13
+ lambda {
14
+ 3.times do
15
+ website.content_types.should be_empty
16
+ end
17
+ }.should change(Congo::ProxyScoper, :count).by(1)
18
+
19
+ proxy = Congo::ProxyScoper.first
20
+ proxy.ext_id.should == 42
21
+ proxy.ext_type.should == 'Website'
22
+
23
+ another = Website.new(43, 'Another simple website')
24
+ lambda {
25
+ another.content_types.should be_empty
26
+ }.should change(Congo::ProxyScoper, :count).by(1)
27
+ end
28
+
29
+ it 'should be used inside a MongoMapper document' do
30
+ account = Account.create(:email => 'layne_stanley@acme.org')
31
+ account.content_types.should be_empty
32
+ end
33
+
34
+ it 'should not break method_missing stuff from the scoper' do
35
+ website = Website.new(42, 'My simple website')
36
+ website.foo?.should == 'Hello foo !'
37
+ lambda {
38
+ website.bar
39
+ }.should raise_error
40
+ end
41
+
42
+ ## Create content types ##
43
+ it 'should create a content type and use it straight from an AR object' do
44
+ website = Website.new(42, 'My simple website')
45
+
46
+ lambda {
47
+ create_blog_post_type(website)
48
+ }.should change(Congo::ContentType, :count).by(1)
49
+
50
+ website.content_types.should_not be_empty
51
+
52
+ website.blog_posts.count.should == 0
53
+ end
54
+
55
+ it 'should create a content type and use it straight from the MM document' do
56
+ account = Account.create!(:email => 'layne_stanley@acme.org')
57
+
58
+ lambda {
59
+ type = create_project_type(account)
60
+ type.slug.should == 'my_projects'
61
+ }.should change(Congo::ContentType, :count).by(1)
62
+
63
+ account.projects.count.should == 0
64
+ account.my_projects.count.should == 0
65
+ end
66
+
67
+ ## Content type validation (own file ?) ##
68
+ it 'should not create a content type if it does not have a name' do
69
+ account = Account.create(:email => 'layne_stanley@acme.org')
70
+ lambda {
71
+ content_type = account.content_types.create(:metadata_keys => [ { :name => 'Title' } ])
72
+ content_type.errors.on(:name).should_not be_nil
73
+ }.should_not change(Congo::ContentType, :count).by(1)
74
+ end
75
+
76
+ it 'should not create a content type if it does not have keys' do
77
+ account = Account.create(:email => 'layne_stanley@acme.org')
78
+ lambda {
79
+ content_type = account.content_types.create(:name => 'Project')
80
+ content_type.errors.on(:metadata_keys).should_not be_nil
81
+ }.should_not change(Congo::ContentType, :count).by(1)
82
+ end
83
+
84
+ ## Collections: add items, ...etc ##
85
+ it 'should add items into a collection' do
86
+ account = Account.create(:email => 'layne_stanley@acme.org')
87
+ create_project_type(account)
88
+
89
+ project = account.projects.build(:name => 'Congo', :description => 'bla bla')
90
+ project.save
91
+ project.respond_to?(:created_at).should == true
92
+ project.respond_to?(:updated_at).should == true
93
+ account.projects.count.should == 1
94
+
95
+ account.send(:projects).create(:name => 'CongoCMS', :description => 'bla bla')
96
+ account.projects.count.should == 2
97
+ end
98
+
99
+ it 'should add items into a collection thru the ProxyScoper' do
100
+ website = Website.new(42, 'My simple website')
101
+ create_blog_post_type(website)
102
+
103
+ website.blog_posts.create(:title => 'Hello world', :body => 'bla bla')
104
+ website.blog_posts.count.should == 1
105
+ end
106
+
107
+ def create_blog_post_type(website)
108
+ website.content_types.create!(:name => 'BlogPost',
109
+ :embedded => false,
110
+ :timestamps => true,
111
+ :metadata_keys => [
112
+ { :name => 'title' },
113
+ { :name => 'body' },
114
+ { :name => 'tags', :type => 'Array' }
115
+ ])
116
+ end
117
+
118
+ def create_project_type(account)
119
+ account.content_types.create!(:name => 'Project',
120
+ :collection_name => 'My projects',
121
+ :embedded => false,
122
+ :metadata_keys => [
123
+ { :name => 'name' },
124
+ { :name => 'description' }
125
+ ])
126
+ end
127
+
128
+ end
@@ -0,0 +1,90 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/functional_spec_helper')
2
+
3
+ describe 'Validation' do
4
+
5
+ before(:each) do
6
+ Account.destroy_all
7
+ Congo::ContentType.destroy_all
8
+
9
+ @account = Account.create(:email => 'layne_stanley@acme.org')
10
+
11
+ create_content_type
12
+ end
13
+
14
+ it 'should be valid' do
15
+ developer = build_developer
16
+ developer.should be_valid
17
+ end
18
+
19
+ it 'should not valid if a key (String type) is not present' do
20
+ developer = build_developer(:name => nil)
21
+ developer.should_not be_valid
22
+ developer.errors.on(:name).should_not be_empty
23
+ end
24
+
25
+ it 'should not valid if a key (File type) is not present' do
26
+ developer = build_developer(:picture => nil)
27
+ developer.should_not be_valid
28
+ developer.errors.on(:picture).should_not be_empty
29
+ end
30
+
31
+ it 'should not valid if a key (Email type) is not present' do
32
+ developer = build_developer(:email => nil)
33
+ developer.should_not be_valid
34
+ developer.errors.on(:email).should_not be_empty
35
+ end
36
+
37
+ it 'should not valid if email has a wrong format' do
38
+ developer = build_developer(:email => 'foo@foo')
39
+ developer.should_not be_valid
40
+ developer.errors.on(:email).should_not be_empty
41
+ end
42
+
43
+ it 'should not valid if a key has a wrong format' do
44
+ developer = build_developer(:blog_url => 'http:/foo.com')
45
+ developer.should_not be_valid
46
+ developer.errors.on(:blog_url).should_not be_empty
47
+ end
48
+
49
+ def build_developer(options = {})
50
+ @account.developers.create({
51
+ :name => 'Layne Stanley',
52
+ :bio => 'foo bar',
53
+ :picture => open_file('avatar.jpeg'),
54
+ :blog_url => 'http://myblog.com',
55
+ :email => 'layne.stanley@aic.com'
56
+ }.merge(options))
57
+ end
58
+
59
+ def create_content_type(options = {})
60
+ type = build_content_type
61
+ type.save!
62
+ type
63
+ end
64
+
65
+ def build_content_type(options = {})
66
+ default_options = {
67
+ :name => 'Developer',
68
+ :embedded => false,
69
+ :metadata_keys => [
70
+ { :name => 'name', :type => 'String' },
71
+ { :name => 'bio', :type => 'Text' },
72
+ { :name => 'picture', :type => 'File' },
73
+ { :name => 'blog_url', :type => 'String' },
74
+ { :name => 'email', :type => 'Email' }
75
+ ],
76
+ :metadata_validations => [
77
+ { :key => 'name', :type => 'presence_of' },
78
+ { :key => 'email', :type => 'presence_of' },
79
+ { :key => 'picture', :type => 'presence_of' },
80
+ { :key => 'blog_url', :type => 'presence_of' },
81
+ { :key => 'blog_url', :type => 'format_of', :argument => '^http:\/\/(.*)$' }
82
+ ] }
83
+
84
+ @account.content_types.build(default_options.merge(options))
85
+ end
86
+
87
+ def open_file(name)
88
+ File.open(File.join(File.dirname(__FILE__), '..', 'assets', name))
89
+ end
90
+ end
@@ -0,0 +1,41 @@
1
+ class WebsiteParent
2
+
3
+ def method_missing(method, *args)
4
+ if method.to_s == 'foo?'
5
+ "Hello foo !"
6
+ else
7
+ super
8
+ end
9
+ end
10
+
11
+ end
12
+
13
+ class Website < WebsiteParent
14
+
15
+ acts_as_congo_scoper
16
+
17
+ attr_accessor :id, :title
18
+
19
+ def initialize(id, title)
20
+ self.id, self.title = id, title
21
+ end
22
+
23
+ def pages
24
+ []
25
+ end
26
+
27
+ end
28
+
29
+ class Account
30
+
31
+ include MongoMapper::Document
32
+
33
+ acts_as_congo_scoper
34
+
35
+ key :email, String
36
+
37
+ def people
38
+ []
39
+ end
40
+
41
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'rubygems'
5
+ require 'mocha'
6
+ require 'mongo_mapper'
7
+
8
+ require 'congo'
9
+ require 'models'
10
+ require 'spec'
11
+ require 'spec/autorun'
12
+
13
+ TEST_DIR = File.expand_path(File.dirname(__FILE__) + '/../tmp')
14
+
15
+ FileUtils.mkdir_p(TEST_DIR) unless File.exist?(TEST_DIR)
16
+
17
+
18
+ I18n.load_path << Dir[File.join(File.dirname(__FILE__), '..', 'config', 'locales', '*.{rb,yml}') ]
19
+ I18n.default_locale = :en
20
+
21
+ Spec::Runner.configure do |config|
22
+ config.mock_with :mocha
23
+ end
@@ -0,0 +1,63 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/unit_spec_helper')
2
+
3
+ describe 'Key' do
4
+
5
+ it 'should be valid' do
6
+ Congo::Metadata::Key.new(:name => 'title').should be_valid
7
+ end
8
+
9
+ it 'should not be valid without a name' do
10
+ key = Congo::Metadata::Key.new(:name => '')
11
+ key.should_not be_valid
12
+ key.errors.on(:name).should_not be_nil
13
+ end
14
+
15
+ it 'should be valid without a name if a label is provided' do
16
+ key = Congo::Metadata::Key.new(:name => '', :label => 'My title')
17
+ key.should be_valid
18
+ key.name.should == "my_title"
19
+ end
20
+
21
+ it 'should be valid without a type' do
22
+ key = Congo::Metadata::Key.new(:name => 'title', :type => nil)
23
+ key.should be_valid
24
+ key.type.should == "String"
25
+ end
26
+
27
+ it 'should have a valid name' do
28
+ key = Congo::Metadata::Key.new(:name => 'Title')
29
+ key.should be_valid
30
+ key.name.should == "title"
31
+ end
32
+
33
+ describe "when applied" do
34
+ after do
35
+ @key.apply(@base, @scope)
36
+ end
37
+
38
+ before do
39
+ @base = mock('Base class')
40
+ @scope = mock('Scope')
41
+ @const = Class.new(String)
42
+ @scope.stubs(:content_type_as_const).with('Key').returns(@const)
43
+ @key = Congo::Metadata::Key.new(:name => 'mykey', :type => 'Key')
44
+ end
45
+
46
+ it "should create the key in the base class" do
47
+ @base.expects(:key).with(:mykey, @const)
48
+ end
49
+
50
+ describe "with validations" do
51
+ before do
52
+ @const.send(:include, Validatable)
53
+ @const.send(:validates_format_of, :to_s, :with => /[0-9]+/)
54
+ @scope.stubs(:content_type_as_const).with('Key').returns(@const)
55
+ @base.stubs(:key).with(:mykey, @const)
56
+ end
57
+
58
+ it "should use include_errors_from in the base class" do
59
+ @base.expects(:include_errors_from).with(:mykey)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/unit_spec_helper')
2
+
3
+ describe Congo::Scoper::InstanceMethods do
4
+ before do
5
+ @instance = Object.new
6
+ @instance.metaclass.send(:include, Congo::Scoper::InstanceMethods)
7
+ @instance.stubs(:content_types).returns(stub_everything)
8
+ end
9
+
10
+ describe "content_type_as_const" do
11
+ it "should support ruby types" do
12
+ @instance.content_type_as_const('String').should == String
13
+ end
14
+
15
+ it "should support Congo Email type" do
16
+ @instance.content_type_as_const('Email').should == Congo::Types::Email
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ MongoMapper.connection = nil
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: congo
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Rodrigo Alvarez
13
+ - Didier Lafforgue
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-04-07 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 2
31
+ - 9
32
+ version: 1.2.9
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: mongo_mapper
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ - 7
45
+ - 2
46
+ version: 0.7.2
47
+ type: :development
48
+ version_requirements: *id002
49
+ description:
50
+ email:
51
+ - papipo@gmail.com
52
+ - didier@nocoffee.fr
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - LICENSE
59
+ - README.rdoc
60
+ files:
61
+ - .gitignore
62
+ - LICENSE
63
+ - README.rdoc
64
+ - Rakefile
65
+ - VERSION
66
+ - config/locales/en.yml
67
+ - config/locales/fr.yml
68
+ - init.rb
69
+ - lib/congo.rb
70
+ - lib/congo/content_type.rb
71
+ - lib/congo/grip/attachment.rb
72
+ - lib/congo/grip/has_attachment.rb
73
+ - lib/congo/list.rb
74
+ - lib/congo/metadata/association.rb
75
+ - lib/congo/metadata/key.rb
76
+ - lib/congo/metadata/validation.rb
77
+ - lib/congo/migration.rb
78
+ - lib/congo/proxy_scoper.rb
79
+ - lib/congo/scoper.rb
80
+ - lib/congo/support.rb
81
+ - lib/congo/types.rb
82
+ - lib/congo/validation.rb
83
+ - spec/assets/avatar.jpeg
84
+ - spec/assets/dhh.jpg
85
+ - spec/functional/content_type_spec.rb
86
+ - spec/functional/functional_spec_helper.rb
87
+ - spec/functional/grip_spec.rb
88
+ - spec/functional/key_spec.rb
89
+ - spec/functional/list_spec.rb
90
+ - spec/functional/migration_spec.rb
91
+ - spec/functional/scoper_spec.rb
92
+ - spec/functional/validation_spec.rb
93
+ - spec/models.rb
94
+ - spec/spec.opts
95
+ - spec/spec_helper.rb
96
+ - spec/unit/key_spec.rb
97
+ - spec/unit/scoper_spec.rb
98
+ - spec/unit/unit_spec_helper.rb
99
+ has_rdoc: true
100
+ homepage: http://github.com/Papipo/congo
101
+ licenses: []
102
+
103
+ post_install_message:
104
+ rdoc_options:
105
+ - --charset=UTF-8
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ requirements: []
123
+
124
+ rubyforge_project:
125
+ rubygems_version: 1.3.6
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: Library to define flexible schemas for mongodb documents.
129
+ test_files:
130
+ - spec/functional/content_type_spec.rb
131
+ - spec/functional/functional_spec_helper.rb
132
+ - spec/functional/grip_spec.rb
133
+ - spec/functional/key_spec.rb
134
+ - spec/functional/list_spec.rb
135
+ - spec/functional/migration_spec.rb
136
+ - spec/functional/scoper_spec.rb
137
+ - spec/functional/validation_spec.rb
138
+ - spec/models.rb
139
+ - spec/spec_helper.rb
140
+ - spec/unit/key_spec.rb
141
+ - spec/unit/scoper_spec.rb
142
+ - spec/unit/unit_spec_helper.rb