openteam-modest_model 0.1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +72 -0
  3. data/Rakefile +35 -0
  4. data/lib/modest_model.rb +12 -0
  5. data/lib/modest_model/base.rb +69 -0
  6. data/lib/modest_model/callbacks.rb +42 -0
  7. data/lib/modest_model/combined_attr.rb +9 -0
  8. data/lib/modest_model/resource.rb +9 -0
  9. data/lib/modest_model/tenacity.rb +181 -0
  10. data/lib/modest_model/validators.rb +9 -0
  11. data/test/compliance_test.rb +26 -0
  12. data/test/dummy/Rakefile +7 -0
  13. data/test/dummy/app/assets/javascripts/application.js +9 -0
  14. data/test/dummy/app/assets/stylesheets/application.css +7 -0
  15. data/test/dummy/app/controllers/application_controller.rb +3 -0
  16. data/test/dummy/app/helpers/application_helper.rb +2 -0
  17. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  18. data/test/dummy/config.ru +4 -0
  19. data/test/dummy/config/application.rb +42 -0
  20. data/test/dummy/config/boot.rb +10 -0
  21. data/test/dummy/config/database.yml +10 -0
  22. data/test/dummy/config/environment.rb +5 -0
  23. data/test/dummy/config/environments/development.rb +27 -0
  24. data/test/dummy/config/environments/production.rb +51 -0
  25. data/test/dummy/config/environments/test.rb +39 -0
  26. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  27. data/test/dummy/config/initializers/inflections.rb +10 -0
  28. data/test/dummy/config/initializers/mime_types.rb +5 -0
  29. data/test/dummy/config/initializers/secret_token.rb +7 -0
  30. data/test/dummy/config/initializers/session_store.rb +8 -0
  31. data/test/dummy/config/initializers/wrap_parameters.rb +12 -0
  32. data/test/dummy/config/locales/en.yml +5 -0
  33. data/test/dummy/config/routes.rb +58 -0
  34. data/test/dummy/public/404.html +26 -0
  35. data/test/dummy/public/422.html +26 -0
  36. data/test/dummy/public/500.html +26 -0
  37. data/test/dummy/public/favicon.ico +0 -0
  38. data/test/dummy/script/rails +6 -0
  39. data/test/fixtures/sample_model.rb +4 -0
  40. data/test/fixtures/sample_resource.rb +45 -0
  41. data/test/modest_model_test.rb +49 -0
  42. data/test/sample_resource_test.rb +153 -0
  43. data/test/test_helper.rb +10 -0
  44. metadata +160 -0
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ <p>We've been notified about this issue and we'll take a look at it shortly.</p>
24
+ </div>
25
+ </body>
26
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ class SampleModel < ModestModel::Base
2
+ attributes :name, :email
3
+ attribute :nickname, :absence => true
4
+ end
@@ -0,0 +1,45 @@
1
+ class SampleResource < ModestModel::Resource
2
+ attributes :id, :name, :email
3
+ attributes :saved_at, :destroyed_at
4
+ attributes :find_callback, :create_callback, :save_callback, :update_callback, :destroy_callback
5
+
6
+ # Attributes with validations
7
+ attribute :nickname, :absence => true
8
+ attribute :number, :numericality => {:allow_blank => true}
9
+
10
+ after_find :set_find_callback
11
+ def set_find_callback
12
+ self.find_callback = true
13
+ end
14
+
15
+ after_create do
16
+ self.create_callback = true
17
+ end
18
+
19
+ after_update do
20
+ self.update_callback = true
21
+ end
22
+
23
+
24
+ after_save do
25
+ self.save_callback = true
26
+ end
27
+
28
+ after_destroy do
29
+ self.destroy_callback = true
30
+ end
31
+
32
+ find do
33
+ # Some call
34
+ self.attributes = {:name => "User", :email => "user@example.com"}
35
+ end
36
+
37
+ save do
38
+ self.saved_at = Time.now
39
+ end
40
+
41
+ destroy do
42
+ self.destroyed_at = Time.now
43
+ end
44
+
45
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+ require 'fixtures/sample_model'
3
+
4
+ class ModestModelTest < ActiveSupport::TestCase
5
+ test "validates absence of nickname" do
6
+ sample = SampleModel.new(:nickname => "Spam")
7
+ assert !sample.valid?
8
+ assert_equal ["is invalid"], sample.errors[:nickname]
9
+ end
10
+
11
+ test "can retrieve all attributes values" do
12
+ sample = SampleModel.new
13
+ sample.name = "John Doe"
14
+ sample.email = "john.doe@example.com"
15
+ assert_equal "John Doe", sample.attributes["name"]
16
+ assert_equal "john.doe@example.com", sample.attributes["email"]
17
+ end
18
+
19
+ test 'sample mail can ask if an attribute is present or not' do
20
+ sample = SampleModel.new
21
+ assert !sample.name?
22
+
23
+ sample.name = "User"
24
+ assert sample.name?
25
+
26
+ sample.email = ""
27
+ assert !sample.email?
28
+ end
29
+
30
+ test 'sample mail can clear attributes using clear_ prefix' do
31
+ sample = SampleModel.new
32
+ sample.name = "User"
33
+ sample.email = "user@example.com"
34
+ assert_equal "User", sample.name
35
+ assert_equal "user@example.com", sample.email
36
+ sample.clear_name
37
+ sample.clear_email
38
+ assert_nil sample.name
39
+ assert_nil sample.email
40
+ end
41
+
42
+ test 'sample mail has name and email as attributes' do
43
+ sample = SampleModel.new
44
+ sample.name = "User"
45
+ assert_equal "User", sample.name
46
+ sample.email = "user@example.com"
47
+ assert_equal "user@example.com", sample.email
48
+ end
49
+ end
@@ -0,0 +1,153 @@
1
+ require 'test_helper'
2
+ require 'fixtures/sample_resource'
3
+
4
+ class SampleResourceTest < ActiveSupport::TestCase
5
+
6
+ # Test the base functionality works!
7
+
8
+ test "validates absence of nickname" do
9
+ sample = SampleResource.new(:nickname => "Spam")
10
+ assert !sample.valid?
11
+ assert_equal ["is invalid"], sample.errors[:nickname]
12
+ end
13
+
14
+ test "can retrieve all attributes values" do
15
+ sample = SampleResource.new
16
+ sample.name = "John Doe"
17
+ sample.email = "john.doe@example.com"
18
+ assert_equal "John Doe", sample.attributes["name"]
19
+ assert_equal "john.doe@example.com", sample.attributes["email"]
20
+ end
21
+
22
+ test 'sample mail can ask if an attribute is present or not' do
23
+ sample = SampleResource.new
24
+ assert !sample.name?
25
+
26
+ sample.name = "User"
27
+ assert sample.name?
28
+
29
+ sample.email = ""
30
+ assert !sample.email?
31
+ end
32
+
33
+ test 'sample mail can clear attributes using clear_ prefix' do
34
+ sample = SampleResource.new
35
+ sample.name = "User"
36
+ sample.email = "user@example.com"
37
+ assert_equal "User", sample.name
38
+ assert_equal "user@example.com", sample.email
39
+ sample.clear_name
40
+ sample.clear_email
41
+ assert_nil sample.name
42
+ assert_nil sample.email
43
+ end
44
+
45
+ test 'sample mail has name and email as attributes' do
46
+ sample = SampleResource.new
47
+ sample.name = "User"
48
+ assert_equal "User", sample.name
49
+ sample.email = "user@example.com"
50
+ assert_equal "user@example.com", sample.email
51
+ end
52
+
53
+ # Testing the resource ability
54
+
55
+ test "the create method returns a newly created object" do
56
+ sample = SampleResource.create(:name => 'User', :email => 'user@example.com')
57
+ assert_equal false, sample.new_record?
58
+ assert_equal "User", sample.name
59
+ assert_equal "user@example.com", sample.email
60
+ end
61
+
62
+ test "the find method returns a record with the id set" do
63
+ sample = SampleResource.find(1)
64
+ assert_equal 1, sample.id
65
+ assert_equal "User", sample.name
66
+ assert_equal "user@example.com", sample.email
67
+ end
68
+
69
+ test "the save method perfoms its action correctly" do
70
+ sample = SampleResource.find(1)
71
+ assert_equal nil, sample.saved_at
72
+ sample.save
73
+ assert_equal Time.now.to_s, sample.saved_at.to_s
74
+ end
75
+
76
+ test "the destroy method perfoms its action correctly" do
77
+ sample = SampleResource.find(1)
78
+ assert_equal nil, sample.destroyed_at
79
+ assert_equal false, sample.destroyed?
80
+ sample.destroy
81
+ assert_equal true, sample.destroyed?
82
+ assert_equal Time.now.to_s, sample.destroyed_at.to_s
83
+ end
84
+
85
+ test "setting the primary key sets a new attribute and removes the old one" do
86
+ sample = SampleResource.find(1)
87
+ assert_equal false, SampleResource.method_defined?(:new_id)
88
+ assert_equal 1, sample.id
89
+ SampleResource.set_primary_key :new_id
90
+ sample = SampleResource.find(1)
91
+ assert_equal false, SampleResource.method_defined?(:id)
92
+ assert_equal 1, sample.new_id
93
+ # Teardown
94
+ SampleResource.set_primary_key :id
95
+ end
96
+
97
+ # Testing the validations on save
98
+
99
+ test "the validations work on save not just valid?" do
100
+ sample = SampleResource.new(:number => 'INVALID')
101
+ assert_equal false, sample.save
102
+ assert_equal ["is not a number"], sample.errors[:number]
103
+
104
+ sample = SampleResource.create(:number => 'INVALID')
105
+ assert_equal ["is not a number"], sample.errors[:number]
106
+
107
+ sample = SampleResource.new
108
+ assert_equal false, sample.update_attributes(:number => 'INVALID')
109
+ assert_equal ["is not a number"], sample.errors[:number]
110
+ end
111
+
112
+ # Testing the callbacks
113
+
114
+ test "the find callback works" do
115
+ sample = SampleResource.new
116
+ assert_equal nil, sample.find_callback
117
+ sample = SampleResource.find(1)
118
+ assert_equal true, sample.find_callback
119
+ end
120
+
121
+ test "the create callback works" do
122
+ sample = SampleResource.new
123
+ assert_equal nil, sample.create_callback
124
+ sample = SampleResource.create
125
+ assert_equal true, sample.create_callback
126
+ end
127
+
128
+ test "the save callback works" do
129
+ sample = SampleResource.new
130
+ assert_equal nil, sample.save_callback
131
+ sample.save
132
+ assert_equal true, sample.save_callback
133
+ sample = SampleResource.create
134
+ assert_equal true, sample.save_callback
135
+ sample.update_attribute :save_callback, false
136
+ assert_equal true, sample.save_callback
137
+ end
138
+
139
+ test "the update callback works" do
140
+ sample = SampleResource.find(1)
141
+ assert_equal nil, sample.update_callback
142
+ sample.update_attribute :update_callback, false
143
+ assert_equal true, sample.update_callback
144
+ end
145
+
146
+ test "the destroy callback works" do
147
+ sample = SampleResource.find(1)
148
+ assert_equal nil, sample.destroy_callback
149
+ sample.destroy
150
+ assert_equal true, sample.destroy_callback
151
+ end
152
+
153
+ end
@@ -0,0 +1,10 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+ require "rails/test_help"
6
+
7
+ Rails.backtrace_cleaner.remove_silencers!
8
+
9
+ # Load support files
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: openteam-modest_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mike Fulcher
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: &17192860 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *17192860
25
+ - !ruby/object:Gem::Dependency
26
+ name: rails
27
+ requirement: &17191700 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 3.1.rc5
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *17191700
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3
38
+ requirement: &17191200 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *17191200
47
+ description: Simple, tableless ActiveModel-compliant models. Like ActiveRecord models
48
+ without the database.
49
+ email:
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - lib/modest_model/callbacks.rb
55
+ - lib/modest_model/resource.rb
56
+ - lib/modest_model/validators.rb
57
+ - lib/modest_model/tenacity.rb
58
+ - lib/modest_model/base.rb
59
+ - lib/modest_model/combined_attr.rb
60
+ - lib/modest_model.rb
61
+ - MIT-LICENSE
62
+ - Rakefile
63
+ - README.md
64
+ - test/sample_resource_test.rb
65
+ - test/compliance_test.rb
66
+ - test/modest_model_test.rb
67
+ - test/dummy/Rakefile
68
+ - test/dummy/config.ru
69
+ - test/dummy/script/rails
70
+ - test/dummy/app/assets/stylesheets/application.css
71
+ - test/dummy/app/assets/javascripts/application.js
72
+ - test/dummy/app/helpers/application_helper.rb
73
+ - test/dummy/app/controllers/application_controller.rb
74
+ - test/dummy/app/views/layouts/application.html.erb
75
+ - test/dummy/public/500.html
76
+ - test/dummy/public/422.html
77
+ - test/dummy/public/404.html
78
+ - test/dummy/public/favicon.ico
79
+ - test/dummy/config/initializers/inflections.rb
80
+ - test/dummy/config/initializers/mime_types.rb
81
+ - test/dummy/config/initializers/session_store.rb
82
+ - test/dummy/config/initializers/wrap_parameters.rb
83
+ - test/dummy/config/initializers/backtrace_silencers.rb
84
+ - test/dummy/config/initializers/secret_token.rb
85
+ - test/dummy/config/boot.rb
86
+ - test/dummy/config/environment.rb
87
+ - test/dummy/config/environments/production.rb
88
+ - test/dummy/config/environments/development.rb
89
+ - test/dummy/config/environments/test.rb
90
+ - test/dummy/config/routes.rb
91
+ - test/dummy/config/database.yml
92
+ - test/dummy/config/locales/en.yml
93
+ - test/dummy/config/application.rb
94
+ - test/fixtures/sample_model.rb
95
+ - test/fixtures/sample_resource.rb
96
+ - test/test_helper.rb
97
+ homepage: https://github.com/6twenty/modest_model
98
+ licenses: []
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ segments:
110
+ - 0
111
+ hash: 1691084445079851857
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ segments:
119
+ - 0
120
+ hash: 1691084445079851857
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 1.8.11
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: Simple, tableless ActiveModel-compliant models
127
+ test_files:
128
+ - test/sample_resource_test.rb
129
+ - test/compliance_test.rb
130
+ - test/modest_model_test.rb
131
+ - test/dummy/Rakefile
132
+ - test/dummy/config.ru
133
+ - test/dummy/script/rails
134
+ - test/dummy/app/assets/stylesheets/application.css
135
+ - test/dummy/app/assets/javascripts/application.js
136
+ - test/dummy/app/helpers/application_helper.rb
137
+ - test/dummy/app/controllers/application_controller.rb
138
+ - test/dummy/app/views/layouts/application.html.erb
139
+ - test/dummy/public/500.html
140
+ - test/dummy/public/422.html
141
+ - test/dummy/public/404.html
142
+ - test/dummy/public/favicon.ico
143
+ - test/dummy/config/initializers/inflections.rb
144
+ - test/dummy/config/initializers/mime_types.rb
145
+ - test/dummy/config/initializers/session_store.rb
146
+ - test/dummy/config/initializers/wrap_parameters.rb
147
+ - test/dummy/config/initializers/backtrace_silencers.rb
148
+ - test/dummy/config/initializers/secret_token.rb
149
+ - test/dummy/config/boot.rb
150
+ - test/dummy/config/environment.rb
151
+ - test/dummy/config/environments/production.rb
152
+ - test/dummy/config/environments/development.rb
153
+ - test/dummy/config/environments/test.rb
154
+ - test/dummy/config/routes.rb
155
+ - test/dummy/config/database.yml
156
+ - test/dummy/config/locales/en.yml
157
+ - test/dummy/config/application.rb
158
+ - test/fixtures/sample_model.rb
159
+ - test/fixtures/sample_resource.rb
160
+ - test/test_helper.rb