sequenced 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/.gitignore +9 -0
  2. data/Gemfile +17 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +163 -0
  5. data/Rakefile +38 -0
  6. data/lib/sequenced.rb +4 -0
  7. data/lib/sequenced/acts_as_sequenced.rb +117 -0
  8. data/lib/sequenced/exceptions.rb +4 -0
  9. data/lib/sequenced/version.rb +3 -0
  10. data/sequenced.gemspec +20 -0
  11. data/test/dummy/README.rdoc +261 -0
  12. data/test/dummy/Rakefile +7 -0
  13. data/test/dummy/app/assets/javascripts/application.js +15 -0
  14. data/test/dummy/app/assets/stylesheets/application.css +13 -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/mailers/.gitkeep +0 -0
  18. data/test/dummy/app/models/.gitkeep +0 -0
  19. data/test/dummy/app/models/account.rb +6 -0
  20. data/test/dummy/app/models/address.rb +4 -0
  21. data/test/dummy/app/models/answer.rb +4 -0
  22. data/test/dummy/app/models/comment.rb +8 -0
  23. data/test/dummy/app/models/invoice.rb +4 -0
  24. data/test/dummy/app/models/order.rb +4 -0
  25. data/test/dummy/app/models/question.rb +4 -0
  26. data/test/dummy/app/models/subscription.rb +3 -0
  27. data/test/dummy/app/models/user.rb +4 -0
  28. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  29. data/test/dummy/config.ru +4 -0
  30. data/test/dummy/config/application.rb +56 -0
  31. data/test/dummy/config/boot.rb +10 -0
  32. data/test/dummy/config/database.yml +25 -0
  33. data/test/dummy/config/environment.rb +5 -0
  34. data/test/dummy/config/environments/development.rb +37 -0
  35. data/test/dummy/config/environments/production.rb +67 -0
  36. data/test/dummy/config/environments/test.rb +37 -0
  37. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  38. data/test/dummy/config/initializers/inflections.rb +15 -0
  39. data/test/dummy/config/initializers/mime_types.rb +5 -0
  40. data/test/dummy/config/initializers/secret_token.rb +7 -0
  41. data/test/dummy/config/initializers/session_store.rb +8 -0
  42. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  43. data/test/dummy/config/locales/en.yml +5 -0
  44. data/test/dummy/config/routes.rb +58 -0
  45. data/test/dummy/db/migrate/20120219165346_create_questions.rb +10 -0
  46. data/test/dummy/db/migrate/20120219165548_create_answers.rb +13 -0
  47. data/test/dummy/db/migrate/20120219171957_create_accounts.rb +9 -0
  48. data/test/dummy/db/migrate/20120219172039_create_invoices.rb +12 -0
  49. data/test/dummy/db/migrate/20120219172922_create_orders.rb +10 -0
  50. data/test/dummy/db/migrate/20120219174931_create_subscriptions.rb +10 -0
  51. data/test/dummy/db/migrate/20120219175744_create_users.rb +12 -0
  52. data/test/dummy/db/migrate/20120219232323_create_addresses.rb +9 -0
  53. data/test/dummy/db/migrate/20120220000804_create_comments.rb +12 -0
  54. data/test/dummy/db/schema.rb +74 -0
  55. data/test/dummy/lib/assets/.gitkeep +0 -0
  56. data/test/dummy/log/.gitkeep +0 -0
  57. data/test/dummy/public/404.html +26 -0
  58. data/test/dummy/public/422.html +26 -0
  59. data/test/dummy/public/500.html +25 -0
  60. data/test/dummy/public/favicon.ico +0 -0
  61. data/test/dummy/script/rails +6 -0
  62. data/test/sequenced_test.rb +100 -0
  63. data/test/test_helper.rb +13 -0
  64. metadata +224 -0
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: "Hello world"
@@ -0,0 +1,58 @@
1
+ Dummy::Application.routes.draw do
2
+ # The priority is based upon order of creation:
3
+ # first created -> highest priority.
4
+
5
+ # Sample of regular route:
6
+ # match 'products/:id' => 'catalog#view'
7
+ # Keep in mind you can assign values other than :controller and :action
8
+
9
+ # Sample of named route:
10
+ # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
11
+ # This route can be invoked with purchase_url(:id => product.id)
12
+
13
+ # Sample resource route (maps HTTP verbs to controller actions automatically):
14
+ # resources :products
15
+
16
+ # Sample resource route with options:
17
+ # resources :products do
18
+ # member do
19
+ # get 'short'
20
+ # post 'toggle'
21
+ # end
22
+ #
23
+ # collection do
24
+ # get 'sold'
25
+ # end
26
+ # end
27
+
28
+ # Sample resource route with sub-resources:
29
+ # resources :products do
30
+ # resources :comments, :sales
31
+ # resource :seller
32
+ # end
33
+
34
+ # Sample resource route with more complex sub-resources
35
+ # resources :products do
36
+ # resources :comments
37
+ # resources :sales do
38
+ # get 'recent', :on => :collection
39
+ # end
40
+ # end
41
+
42
+ # Sample resource route within a namespace:
43
+ # namespace :admin do
44
+ # # Directs /admin/products/* to Admin::ProductsController
45
+ # # (app/controllers/admin/products_controller.rb)
46
+ # resources :products
47
+ # end
48
+
49
+ # You can have the root of your site routed with "root"
50
+ # just remember to delete public/index.html.
51
+ # root :to => 'welcome#index'
52
+
53
+ # See how all your routes lay out with "rake routes"
54
+
55
+ # This is a legacy wild controller route that's not recommended for RESTful applications.
56
+ # Note: This route will make all actions in every controller accessible via GET requests.
57
+ # match ':controller(/:action(/:id))(.:format)'
58
+ end
@@ -0,0 +1,10 @@
1
+ class CreateQuestions < ActiveRecord::Migration
2
+ def change
3
+ create_table :questions do |t|
4
+ t.string :summary
5
+ t.text :body
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ class CreateAnswers < ActiveRecord::Migration
2
+ def change
3
+ create_table :answers do |t|
4
+ t.references :question
5
+ t.text :body
6
+ t.integer :sequential_id
7
+
8
+ t.timestamps
9
+ end
10
+ add_index :answers, :question_id
11
+ add_index :answers, :sequential_id
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ class CreateAccounts < ActiveRecord::Migration
2
+ def change
3
+ create_table :accounts do |t|
4
+ t.string :name
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ class CreateInvoices < ActiveRecord::Migration
2
+ def change
3
+ create_table :invoices do |t|
4
+ t.integer :amount
5
+ t.integer :sequential_id
6
+ t.references :account
7
+
8
+ t.timestamps
9
+ end
10
+ add_index :invoices, :account_id
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ class CreateOrders < ActiveRecord::Migration
2
+ def change
3
+ create_table :orders do |t|
4
+ t.string :product
5
+ t.references :account
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class CreateSubscriptions < ActiveRecord::Migration
2
+ def change
3
+ create_table :subscriptions do |t|
4
+ t.string :plan
5
+ t.integer :sequential_id
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :users do |t|
4
+ t.references :account
5
+ t.string :name
6
+ t.integer :custom_sequential_id
7
+
8
+ t.timestamps
9
+ end
10
+ add_index :users, :account_id
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ class CreateAddresses < ActiveRecord::Migration
2
+ def change
3
+ create_table :addresses do |t|
4
+ t.references :account
5
+ t.string :city
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ class CreateComments < ActiveRecord::Migration
2
+ def change
3
+ create_table :comments do |t|
4
+ t.references :question
5
+ t.text :body
6
+ t.integer :sequential_id
7
+
8
+ t.timestamps
9
+ end
10
+ add_index :comments, :question_id
11
+ end
12
+ end
@@ -0,0 +1,74 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended to check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(:version => 20120219175744) do
15
+
16
+ create_table "accounts", :force => true do |t|
17
+ t.string "name"
18
+ t.datetime "created_at", :null => false
19
+ t.datetime "updated_at", :null => false
20
+ end
21
+
22
+ create_table "answers", :force => true do |t|
23
+ t.integer "question_id"
24
+ t.text "body"
25
+ t.integer "sequential_id"
26
+ t.datetime "created_at", :null => false
27
+ t.datetime "updated_at", :null => false
28
+ end
29
+
30
+ add_index "answers", ["question_id"], :name => "index_answers_on_question_id"
31
+ add_index "answers", ["sequential_id"], :name => "index_answers_on_sequential_id"
32
+
33
+ create_table "invoices", :force => true do |t|
34
+ t.integer "amount"
35
+ t.integer "sequential_id"
36
+ t.integer "account_id"
37
+ t.datetime "created_at", :null => false
38
+ t.datetime "updated_at", :null => false
39
+ end
40
+
41
+ add_index "invoices", ["account_id"], :name => "index_invoices_on_account_id"
42
+
43
+ create_table "orders", :force => true do |t|
44
+ t.string "product"
45
+ t.integer "account_id"
46
+ t.datetime "created_at", :null => false
47
+ t.datetime "updated_at", :null => false
48
+ end
49
+
50
+ create_table "questions", :force => true do |t|
51
+ t.string "summary"
52
+ t.text "body"
53
+ t.datetime "created_at", :null => false
54
+ t.datetime "updated_at", :null => false
55
+ end
56
+
57
+ create_table "subscriptions", :force => true do |t|
58
+ t.string "plan"
59
+ t.integer "sequential_id"
60
+ t.datetime "created_at", :null => false
61
+ t.datetime "updated_at", :null => false
62
+ end
63
+
64
+ create_table "users", :force => true do |t|
65
+ t.integer "account_id"
66
+ t.string "name"
67
+ t.integer "custom_sequential_id"
68
+ t.datetime "created_at", :null => false
69
+ t.datetime "updated_at", :null => false
70
+ end
71
+
72
+ add_index "users", ["account_id"], :name => "index_users_on_account_id"
73
+
74
+ end
File without changes
File without changes
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</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/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -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,25 @@
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
+ </div>
24
+ </body>
25
+ </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,100 @@
1
+ require 'test_helper'
2
+
3
+ # Test Models:
4
+ #
5
+ # Answer - :scope => :question_id
6
+ # Comment - :scope => :question_id (with an AR default scope)
7
+ # Invoice - :scope => :account_id, :start_at => 1000
8
+ # Order - :scope => :non_existent_column
9
+ # User - :scope => :account_id, :column => :custom_sequential_id
10
+ # Address - :scope => :account_id ('sequential_id' does not exist)
11
+ # Subscription - no options
12
+
13
+ class SequencedTest < ActiveSupport::TestCase
14
+ test "default start_at" do
15
+ question = Question.create
16
+ answer = question.answers.create
17
+ assert_equal 1, answer.sequential_id
18
+ end
19
+
20
+ test "custom start_at" do
21
+ account = Account.create
22
+ invoice = account.invoices.create
23
+ assert_equal 1000, invoice.sequential_id
24
+
25
+ another_invoice = account.invoices.create
26
+ assert_equal 1001, another_invoice.sequential_id
27
+ end
28
+
29
+ test "custom start_at with populated table" do
30
+ account = Account.create
31
+ account.invoices.create(:sequential_id => 1)
32
+ invoice = account.invoices.create
33
+ assert_equal 1000, invoice.sequential_id
34
+ end
35
+
36
+ test "sequential id increment" do
37
+ question = Question.create
38
+ question.answers.create(:sequential_id => 10)
39
+ another_answer = question.answers.create
40
+ assert_equal 11, another_answer.sequential_id
41
+ end
42
+
43
+ test "default scope" do
44
+ Subscription.create(:sequential_id => 1)
45
+ subscription = Subscription.create
46
+ assert_equal 2, subscription.sequential_id
47
+ end
48
+
49
+ test "undefined scope method" do
50
+ account = Account.create
51
+ order = account.orders.build
52
+ assert_raises(Sequenced::InvalidAttributeError) { order.save }
53
+ end
54
+
55
+ test "scope method returns nil" do
56
+ answer = Answer.new
57
+ assert_raises(Sequenced::InvalidAttributeError) { answer.save }
58
+ end
59
+
60
+ test "custom sequential id column" do
61
+ account = Account.create
62
+ user = account.users.create
63
+ assert_equal 1, user.custom_sequential_id
64
+ end
65
+
66
+ test "sequential id remains on save" do
67
+ question = Question.create
68
+ answer = question.answers.create
69
+ assert_equal 1, answer.sequential_id
70
+
71
+ answer.reload
72
+ answer.body = "Updated body"
73
+ answer.save
74
+ assert_equal 1, answer.sequential_id
75
+ end
76
+
77
+ test "undefined sequential id column" do
78
+ account = Account.create
79
+ address = account.addresses.build
80
+ assert_raises(Sequenced::InvalidAttributeError) { address.save }
81
+ end
82
+
83
+ test "manually setting sequential id" do
84
+ question = Question.create
85
+ answer = question.answers.build(:sequential_id => 10)
86
+ another_answer = question.answers.build(:sequential_id => 10)
87
+ answer.save
88
+ another_answer.save
89
+
90
+ assert_equal 10, answer.sequential_id
91
+ assert_equal 11, another_answer.sequential_id
92
+ end
93
+
94
+ test "model with a default scope for sorting" do
95
+ question = Question.create
96
+ (1..3).each { |id| question.comments.create(:sequential_id => id) }
97
+ comment = question.comments.create
98
+ assert_equal 4, comment.sequential_id
99
+ end
100
+ end