stocktrade-money-rails 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data/CHANGELOG.md +60 -0
  2. data/LICENSE +22 -0
  3. data/README.md +359 -0
  4. data/Rakefile +60 -0
  5. data/lib/generators/money_rails/initializer_generator.rb +15 -0
  6. data/lib/generators/templates/money.rb +41 -0
  7. data/lib/money-rails.rb +12 -0
  8. data/lib/money-rails/active_model/validator.rb +37 -0
  9. data/lib/money-rails/active_record/monetizable.rb +138 -0
  10. data/lib/money-rails/configuration.rb +47 -0
  11. data/lib/money-rails/helpers/action_view_extension.rb +36 -0
  12. data/lib/money-rails/hooks.rb +30 -0
  13. data/lib/money-rails/mongoid/three.rb +42 -0
  14. data/lib/money-rails/mongoid/two.rb +25 -0
  15. data/lib/money-rails/railtie.rb +7 -0
  16. data/lib/money-rails/version.rb +3 -0
  17. data/money-rails.gemspec +30 -0
  18. data/spec/active_record/monetizable_spec.rb +308 -0
  19. data/spec/configuration_spec.rb +20 -0
  20. data/spec/dummy/README.rdoc +261 -0
  21. data/spec/dummy/Rakefile +7 -0
  22. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  23. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  24. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  25. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  26. data/spec/dummy/app/models/dummy_product.rb +10 -0
  27. data/spec/dummy/app/models/priceable.rb +7 -0
  28. data/spec/dummy/app/models/product.rb +26 -0
  29. data/spec/dummy/app/models/service.rb +8 -0
  30. data/spec/dummy/app/models/transaction.rb +13 -0
  31. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  32. data/spec/dummy/config.ru +4 -0
  33. data/spec/dummy/config/application.rb +61 -0
  34. data/spec/dummy/config/boot.rb +10 -0
  35. data/spec/dummy/config/database.yml +25 -0
  36. data/spec/dummy/config/environment.rb +5 -0
  37. data/spec/dummy/config/environments/development.rb +37 -0
  38. data/spec/dummy/config/environments/production.rb +67 -0
  39. data/spec/dummy/config/environments/test.rb +37 -0
  40. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  41. data/spec/dummy/config/initializers/inflections.rb +15 -0
  42. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  43. data/spec/dummy/config/initializers/money.rb +31 -0
  44. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  45. data/spec/dummy/config/initializers/session_store.rb +8 -0
  46. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  47. data/spec/dummy/config/locales/en.yml +5 -0
  48. data/spec/dummy/config/mongoid.yml +23 -0
  49. data/spec/dummy/config/routes.rb +3 -0
  50. data/spec/dummy/db/development.sqlite3 +0 -0
  51. data/spec/dummy/db/migrate/20120331190108_create_products.rb +10 -0
  52. data/spec/dummy/db/migrate/20120402080348_add_bonus_cents_to_product.rb +6 -0
  53. data/spec/dummy/db/migrate/20120524052716_create_services.rb +10 -0
  54. data/spec/dummy/db/migrate/20120528181002_create_transactions.rb +11 -0
  55. data/spec/dummy/db/migrate/20120528210103_create_dummy_products.rb +10 -0
  56. data/spec/dummy/db/migrate/20120607210247_add_column_that_allows_nil.rb +5 -0
  57. data/spec/dummy/db/migrate/20120712202655_add_sale_price_cents_to_product.rb +7 -0
  58. data/spec/dummy/db/schema.rb +49 -0
  59. data/spec/dummy/db/structure.sql +17 -0
  60. data/spec/dummy/db/test.sqlite3 +0 -0
  61. data/spec/dummy/public/404.html +26 -0
  62. data/spec/dummy/public/422.html +26 -0
  63. data/spec/dummy/public/500.html +25 -0
  64. data/spec/dummy/public/favicon.ico +0 -0
  65. data/spec/dummy/script/rails +6 -0
  66. data/spec/helpers/action_view_extension_spec.rb +38 -0
  67. data/spec/helpers/form_helper_spec.rb +19 -0
  68. data/spec/mongoid/three_spec.rb +31 -0
  69. data/spec/mongoid/two_spec.rb +25 -0
  70. data/spec/spec_helper.rb +19 -0
  71. data/spec/support/database_cleaner.rb +14 -0
  72. metadata +272 -0
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ Dummy::Application.config.secret_token = 'a296fb1735faa781e147a1a15f644e9a5c74f9fec357b6d0c94983a0700db240c8c1e020f5ed6413f2025f0092cf8a8db9990ab554ee59964c6ef40fe4ba884a'
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Dummy::Application.config.session_store :cookie_store, :key => '_dummy_session'
4
+
5
+ # Use the database for sessions instead of the cookie-based default,
6
+ # which shouldn't be used to store highly confidential information
7
+ # (create the session table with "rails generate session_migration")
8
+ # Dummy::Application.config.session_store :active_record_store
@@ -0,0 +1,14 @@
1
+ # Be sure to restart your server when you modify this file.
2
+ #
3
+ # This file contains settings for ActionController::ParamsWrapper which
4
+ # is enabled by default.
5
+
6
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7
+ ActiveSupport.on_load(:action_controller) do
8
+ wrap_parameters :format => [:json]
9
+ end
10
+
11
+ # Disable root element in JSON by default.
12
+ ActiveSupport.on_load(:active_record) do
13
+ self.include_root_in_json = false
14
+ end
@@ -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,23 @@
1
+ development:
2
+ # For Mongoid 2.x
3
+ host: localhost
4
+ database: dummy_development
5
+
6
+ # For Mongoid 3.x
7
+ sessions:
8
+ default:
9
+ database: dummy_development
10
+ hosts:
11
+ - localhost:27017
12
+
13
+ test:
14
+ # For Mongoid 2.x
15
+ host: localhost
16
+ database: dummy_test
17
+
18
+ # For Mongoid 3.x
19
+ sessions:
20
+ default:
21
+ database: dummy_test
22
+ hosts:
23
+ - localhost:27017
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+
3
+ end
@@ -0,0 +1,10 @@
1
+ class CreateProducts < ActiveRecord::Migration
2
+ def change
3
+ create_table :products do |t|
4
+ t.integer :price_cents
5
+ t.integer :discount
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ class AddBonusCentsToProduct < ActiveRecord::Migration
2
+ def change
3
+ add_column :products, :bonus_cents, :integer
4
+
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ class CreateServices < ActiveRecord::Migration
2
+ def change
3
+ create_table :services do |t|
4
+ t.integer :charge_cents
5
+ t.integer :discount_cents
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ class CreateTransactions < ActiveRecord::Migration
2
+ def change
3
+ create_table :transactions do |t|
4
+ t.integer :amount_cents
5
+ t.integer :tax_cents
6
+ t.string :currency
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ class CreateDummyProducts < ActiveRecord::Migration
2
+ def change
3
+ create_table :dummy_products do |t|
4
+ t.string :currency
5
+ t.integer :price_cents
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ class AddColumnThatAllowsNil < ActiveRecord::Migration
2
+ def change
3
+ add_column :products, :optional_price_cents, :integer
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ class AddSalePriceCentsToProduct < ActiveRecord::Migration
2
+ def change
3
+ add_column :products, :sale_price_amount, :integer,
4
+ :default => 0, :null => false
5
+ add_column :products, :sale_price_currency_code, :string
6
+ end
7
+ end
@@ -0,0 +1,49 @@
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 => 20120712202655) do
15
+
16
+ create_table "dummy_products", :force => true do |t|
17
+ t.string "currency"
18
+ t.integer "price_cents"
19
+ t.datetime "created_at", :null => false
20
+ t.datetime "updated_at", :null => false
21
+ end
22
+
23
+ create_table "products", :force => true do |t|
24
+ t.integer "price_cents"
25
+ t.integer "discount"
26
+ t.datetime "created_at", :null => false
27
+ t.datetime "updated_at", :null => false
28
+ t.integer "bonus_cents"
29
+ t.integer "optional_price_cents"
30
+ t.integer "sale_price_amount", :default => 0, :null => false
31
+ t.string "sale_price_currency_code"
32
+ end
33
+
34
+ create_table "services", :force => true do |t|
35
+ t.integer "charge_cents"
36
+ t.integer "discount_cents"
37
+ t.datetime "created_at", :null => false
38
+ t.datetime "updated_at", :null => false
39
+ end
40
+
41
+ create_table "transactions", :force => true do |t|
42
+ t.integer "amount_cents"
43
+ t.integer "tax_cents"
44
+ t.string "currency"
45
+ t.datetime "created_at", :null => false
46
+ t.datetime "updated_at", :null => false
47
+ end
48
+
49
+ end
@@ -0,0 +1,17 @@
1
+ CREATE TABLE "dummy_products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "currency" varchar(255), "price_cents" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
2
+ CREATE TABLE "products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "price_cents" integer, "discount" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "bonus_cents" integer, "optional_price_cents" integer);
3
+ CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
4
+ CREATE TABLE "services" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "charge_cents" integer, "discount_cents" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
5
+ CREATE TABLE "transactions" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "amount_cents" integer, "tax_cents" integer, "currency" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
6
+ CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
7
+ INSERT INTO schema_migrations (version) VALUES ('20120331190108');
8
+
9
+ INSERT INTO schema_migrations (version) VALUES ('20120402080348');
10
+
11
+ INSERT INTO schema_migrations (version) VALUES ('20120524052716');
12
+
13
+ INSERT INTO schema_migrations (version) VALUES ('20120528181002');
14
+
15
+ INSERT INTO schema_migrations (version) VALUES ('20120528210103');
16
+
17
+ INSERT INTO schema_migrations (version) VALUES ('20120607210247');
@@ -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,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'MoneyRails::ActionViewExtension' do
4
+ describe '#currency_symbol' do
5
+ subject { helper.currency_symbol }
6
+ it { should be_a String }
7
+ it { should include Money.default_currency.symbol }
8
+ end
9
+
10
+ describe '#humanized_money' do
11
+ subject { helper.humanized_money Money.new(12500) }
12
+ it { should be_a String }
13
+ it { should_not include Money.default_currency.symbol }
14
+ it { should_not include Money.default_currency.decimal_mark }
15
+ end
16
+
17
+ describe '#humanized_money_with_symbol' do
18
+ subject { helper.humanized_money_with_symbol Money.new(12500) }
19
+ it { should be_a String }
20
+ it { should_not include Money.default_currency.decimal_mark }
21
+ it { should include Money.default_currency.symbol }
22
+ end
23
+
24
+ describe '#money_without_cents' do
25
+ subject { helper.money_without_cents Money.new(12500) }
26
+ it { should be_a String }
27
+ it { should_not include Money.default_currency.symbol }
28
+ it { should_not include Money.default_currency.decimal_mark }
29
+ end
30
+
31
+ describe '#money_without_cents_and_with_symbol' do
32
+ subject { helper.money_without_cents_and_with_symbol Money.new(12500) }
33
+ it { should be_a String }
34
+ it { should_not include Money.default_currency.decimal_mark }
35
+ it { should include Money.default_currency.symbol }
36
+ end
37
+
38
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ if defined? ActiveRecord
4
+ describe "Test helper in form blocks" do
5
+
6
+ let :product do
7
+ Product.create(:price_cents => 3000, :discount => 150,
8
+ :bonus_cents => 200, :optional_price => 100,
9
+ :sale_price_amount => 1200)
10
+ end
11
+
12
+ context "textfield" do
13
+ it "uses the current value of money field in textfield" do
14
+ helper.instance_variable_set :@product, product
15
+ helper.text_field(:product, :price).should =~ /value=\"#{product.price.to_s}\"/
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^3(.*)/
4
+
5
+ describe Money do
6
+ let!(:priceable) { Priceable.create(:price => Money.new(100, 'EUR')) }
7
+
8
+ context "mongoize" do
9
+ it "mongoizes correctly a Money object to a hash of cents and currency" do
10
+ priceable.price.cents.should == 100
11
+ priceable.price.currency.should == Money::Currency.find('EUR')
12
+ end
13
+ end
14
+
15
+ context "demongoize" do
16
+ subject { priceable.price }
17
+ it { should be_an_instance_of(Money) }
18
+ it { should == Money.new(100, 'EUR') }
19
+ it "returns nil if a nil value was stored" do
20
+ nil_priceable = Priceable.create(:price => nil)
21
+ nil_priceable.price.should be_nil
22
+ end
23
+ end
24
+
25
+ context "evolve" do
26
+ it "transforms correctly a Money object to a Mongo friendly value" do
27
+ Priceable.where(:price => Money.new(100, 'EUR')).first.should == priceable
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^2(.*)/
4
+
5
+ describe Money do
6
+ let(:priceable) { Priceable.create(:price => Money.new(100, 'EUR')) }
7
+
8
+ context "serialize" do
9
+ it "serializes correctly a Money object to a hash of cents and currency" do
10
+ priceable.price.cents.should == 100
11
+ priceable.price.currency.should == Money::Currency.find('EUR')
12
+ end
13
+ end
14
+
15
+ context "deserialize" do
16
+ subject { priceable.price }
17
+ it { should be_an_instance_of(Money) }
18
+ it { should == Money.new(100, 'EUR') }
19
+ it "returns nil if a nil value was stored" do
20
+ nil_priceable = Priceable.create(:price => nil)
21
+ nil_priceable.price.should be_nil
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,19 @@
1
+ ENV["RAILS_ENV"] = 'test'
2
+
3
+ # Require dummy Rails app
4
+ require File.expand_path("../../spec/dummy/config/environment", __FILE__)
5
+
6
+ require 'database_cleaner'
7
+ require 'rspec/rails'
8
+ require 'rspec/autorun'
9
+
10
+ # Requires supporting ruby files with custom matchers and macros, etc,
11
+ # in spec/support/ and its subdirectories.
12
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
13
+
14
+ RSpec.configure do |config|
15
+ # If true, the base class of anonymous controllers will be inferred
16
+ # automatically. This will be the default behavior in future versions of
17
+ # rspec-rails.
18
+ config.infer_base_class_for_anonymous_controllers = false
19
+ end