Permalink
Browse files

Merge pull request #7 from fellix/master

Formatado campo de valor
  • Loading branch information...
vquaiato committed Mar 1, 2012
2 parents edcc4e2 + 8db20fc commit 63ef8e47055c0e14ba3d6cce88533cbc0d0cd0a3
View
@@ -4,6 +4,7 @@ gem 'rails', '3.2.1'
gem 'pg'
gem 'jquery-rails'
gem 'database_cleaner'
+gem "money"
group :development do
gem 'thin'
View
@@ -47,6 +47,9 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.17.2)
+ money (4.0.2)
+ i18n (~> 0.4)
+ json
multi_json (1.1.0)
pg (0.13.1)
polyglot (0.3.3)
@@ -110,6 +113,7 @@ PLATFORMS
DEPENDENCIES
database_cleaner
jquery-rails
+ money
pg
rails (= 3.2.1)
rspec-rails (= 2.8.1)
@@ -12,4 +12,5 @@
//
//= require jquery
//= require jquery_ujs
+//= require jquery.maskMoney
//= require_tree .
@@ -1,2 +1,5 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
+$(function(){
+ $("#salary_amount").maskMoney({symbol:'R$ ', showSymbol:true, thousands:'.', decimal:',', symbolStay: true, allowZero:false, allowNegative:false})
+});
View
@@ -6,4 +6,12 @@ class Salary < ActiveRecord::Base
def self.grouped_by_cities
salaries = Salary.average(:amount, order: "1 desc", group: :city)
end
+
+ def amount= amount
+ if amount.is_a? String
+ amount = BigDecimal.new(amount.to_money.to_s)
+ amount = nil if amount == 0
+ end
+ write_attribute :amount, amount
+ end
end
@@ -2,14 +2,14 @@ development:
adapter: postgresql
encoding: unicode
database: salarios_development
- username: vquaiato
+ username: postgres
host: localhost
test:
adapter: postgresql
encoding: unicode
database: salarios_test
pool: 5
- username: vquaiato
+ username: postgres
host: localhost
production:
@@ -0,0 +1 @@
+Money.default_currency = Money::Currency.new("BRL")
View
@@ -1,62 +1,7 @@
Salarios::Application.routes.draw do
- # The priority is based upon order of creation:
- # first created -> highest priority.
-
- # Sample of regular route:
- # match 'products/:id' => 'catalog#view'
- # Keep in mind you can assign values other than :controller and :action
-
- # Sample of named route:
- # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
- # This route can be invoked with purchase_url(:id => product.id)
-
- # Sample resource route (maps HTTP verbs to controller actions automatically):
- # resources :products
-
- # Sample resource route with options:
- # resources :products do
- # member do
- # get 'short'
- # post 'toggle'
- # end
- #
- # collection do
- # get 'sold'
- # end
- # end
-
- # Sample resource route with sub-resources:
- # resources :products do
- # resources :comments, :sales
- # resource :seller
- # end
-
- # Sample resource route with more complex sub-resources
- # resources :products do
- # resources :comments
- # resources :sales do
- # get 'recent', :on => :collection
- # end
- # end
-
- # Sample resource route within a namespace:
- # namespace :admin do
- # # Directs /admin/products/* to Admin::ProductsController
- # # (app/controllers/admin/products_controller.rb)
- # resources :products
- # end
-
- # You can have the root of your site routed with "root"
- # just remember to delete public/index.html.
root :to => 'home#index'
resources :salaries do
end
get "/cities/in/:state" => "state#cities", as: :get_cities
- post "/salary/create" => "salaries#create", as: :create_salary
-
- # See how all your routes lay out with "rake routes"
-
- # This is a legacy wild controller route that's not recommended for RESTful applications.
- # Note: This route will make all actions in every controller accessible via GET requests.
- # match ':controller(/:action(/:id))(.:format)'
+ post "/salary" => "salaries#create", as: :create_salary
end
@@ -12,6 +12,12 @@
assigns(:salary).amount.should eq 1000
assigns(:salary).city.should eq city
end
+ it "should create the salary when receive a salary in a js mask" do
+ post :create, salary:{amount: "R$ 1.000,00", city_id: city.id}
+
+ assigns(:salary).amount.should eq 1000
+ assigns(:salary).city.should eq city
+ end
end
context "with and invalid salary" do
@@ -5,6 +5,8 @@
context "properties" do
it { should have_valid(:amount).when(1000) }
it { should have_valid(:amount).when(1000.99) }
+ it { should have_valid(:amount).when("1.000,99") }
+ it { should have_valid(:amount).when("R$ 1.000,99") }
it { should_not have_valid(:amount).when('') }
it { should_not have_valid(:amount).when(nil) }
it { should_not have_valid(:amount).when('salary') }
Oops, something went wrong.

0 comments on commit 63ef8e4

Please sign in to comment.