spree_zaez_brazilian_fields 3.0.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +14 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +12 -0
  5. data/Gemfile +7 -0
  6. data/Guardfile +90 -0
  7. data/LICENSE +26 -0
  8. data/README.md +98 -0
  9. data/Rakefile +21 -0
  10. data/app/assets/javascripts/spree/backend/app/users/user_account_type.js.coffee +79 -0
  11. data/app/assets/javascripts/spree/backend/spree_zaez_brazilian_fields.js +4 -0
  12. data/app/assets/javascripts/spree/frontend/app/users/user_account_type.js.coffee +102 -0
  13. data/app/assets/javascripts/spree/frontend/spree_zaez_brazilian_fields.js +3 -0
  14. data/app/assets/stylesheets/spree/backend/spree_zaez_brazilian_fields.css +14 -0
  15. data/app/assets/stylesheets/spree/frontend/spree_zaez_brazilian_fields.css +5 -0
  16. data/app/helpers/spree/api/api_helpers_decorator.rb +4 -0
  17. data/app/models/spree/app_configuration_decorator.rb +6 -0
  18. data/app/models/spree/user_decorator.rb +87 -0
  19. data/app/overrides/spree/admin/general_settings/edit/add_sms_permission.html.erb.deface +23 -0
  20. data/app/overrides/spree/shared/_user_form/add_brazilian_fields.html.erb.deface +47 -0
  21. data/app/overrides/spree/shared/_user_form/add_name_to_form.html.erb.deface +4 -0
  22. data/app/overrides/spree/user_registrations/new/add_user_account_constructor.html.erb.deface +5 -0
  23. data/app/overrides/spree/users/edit/add_id_to_form.html.erb.deface +2 -0
  24. data/app/overrides/spree/users/edit/add_user_account_constructor.html.erb.deface +5 -0
  25. data/app/overrides/spree/users/show/add_brazilian_fields.html.erb.deface +20 -0
  26. data/app/views/spree/admin/users/_form.html.erb +139 -0
  27. data/bin/rails +7 -0
  28. data/config/locales/en.yml +20 -0
  29. data/config/locales/pt-br.yml +20 -0
  30. data/config/routes.rb +3 -0
  31. data/db/migrate/20150527162516_add_brazilian_fields_to_spree_users.rb +15 -0
  32. data/lib/generators/spree_zaez_brazilian_fields/install/install_generator.rb +31 -0
  33. data/lib/spree_zaez_brazilian_fields.rb +7 -0
  34. data/lib/spree_zaez_brazilian_fields/engine.rb +20 -0
  35. data/lib/spree_zaez_brazilian_fields/factories.rb +25 -0
  36. data/spec/features/account_spec.rb +31 -0
  37. data/spec/features/admin/general_settings_spec.rb +32 -0
  38. data/spec/features/admin/users_spec.rb +150 -0
  39. data/spec/features/sign_up_spec.rb +94 -0
  40. data/spec/models/spree/app_configuration_decorator_spec.rb +9 -0
  41. data/spec/models/spree/user_decorator_spec.rb +97 -0
  42. data/spec/spec_helper.rb +89 -0
  43. data/spec/support/capybara_login.rb +13 -0
  44. data/spec/support/controller_hacks.rb +34 -0
  45. data/spree_zaez_brazilian_fields.gemspec +38 -0
  46. data/vendor/assets/javascripts/inputmask/inputmask.min.js +10 -0
  47. data/vendor/assets/javascripts/inputmask/jquery.inputmask.min.js +8 -0
  48. data/vendor/assets/javascripts/jquery.validate/jquery.validate.min.js +12 -0
  49. metadata +59 -3
@@ -0,0 +1,31 @@
1
+ module SpreeZaezBrazilianFields
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+
5
+ class_option :auto_run_migrations, :type => :boolean, :default => false
6
+
7
+ def add_javascripts
8
+ append_file 'vendor/assets/javascripts/spree/frontend/all.js', "//= require spree/frontend/spree_zaez_brazilian_fields\n"
9
+ append_file 'vendor/assets/javascripts/spree/backend/all.js', "//= require spree/backend/spree_zaez_brazilian_fields\n"
10
+ end
11
+
12
+ def add_stylesheets
13
+ inject_into_file 'vendor/assets/stylesheets/spree/frontend/all.css', " *= require spree/frontend/spree_zaez_brazilian_fields\n", :before => /\*\//, :verbose => true
14
+ inject_into_file 'vendor/assets/stylesheets/spree/backend/all.css', " *= require spree/backend/spree_zaez_brazilian_fields\n", :before => /\*\//, :verbose => true
15
+ end
16
+
17
+ def add_migrations
18
+ run 'bundle exec rake railties:install:migrations FROM=spree_zaez_brazilian_fields'
19
+ end
20
+
21
+ def run_migrations
22
+ run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask 'Would you like to run the migrations now? [Y/n]')
23
+ if run_migrations
24
+ run 'bundle exec rake db:migrate'
25
+ else
26
+ puts 'Skipping rake db:migrate, don\'t forget to run it!'
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ require 'spree_core'
2
+ require 'spree_zaez_brazilian_fields/engine'
3
+ require 'cpf_cnpj'
4
+
5
+ # Permitted attributes for users
6
+ Spree::PermittedAttributes.user_attributes.push :name, :account_type, :newsletter, :receive_sms, :cpf, :rg,
7
+ :birth_date, :gender, :cnpj, :state_registry, :contact
@@ -0,0 +1,20 @@
1
+ module SpreeBrazilianFields
2
+ class Engine < Rails::Engine
3
+ require 'spree/core'
4
+ isolate_namespace Spree
5
+ engine_name 'spree_zaez_brazilian_fields'
6
+
7
+ # use rspec for tests
8
+ config.generators do |g|
9
+ g.test_framework :rspec
10
+ end
11
+
12
+ def self.activate
13
+ Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
14
+ Rails.configuration.cache_classes ? require(c) : load(c)
15
+ end
16
+ end
17
+
18
+ config.to_prepare &method(:activate).to_proc
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ FactoryGirl.define do
2
+
3
+ factory :personal_account, class: Spree.user_class do
4
+ email { generate(:random_email) }
5
+ login { email }
6
+ password 'secret'
7
+ password_confirmation { password }
8
+ account_type 'personal'
9
+ cpf CPF.generate(true)
10
+ rg '123'
11
+ birth_date Date.today
12
+ gender 'f'
13
+ end
14
+
15
+ factory :business_account, class: Spree.user_class do
16
+ email { generate(:random_email) }
17
+ login { email }
18
+ password 'secret'
19
+ password_confirmation { password }
20
+ account_type 'business'
21
+ cnpj CNPJ.generate(true)
22
+ state_registry '123'
23
+ contact 'some name'
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Account user', type: :feature do
4
+ context 'show brazilian fields' do
5
+
6
+ # login into user page
7
+ before do
8
+ @user = Spree.user_class.create(email: 'user@test.com',
9
+ password: 'password',
10
+ password_confirmation: 'password',
11
+ name: 'users name',
12
+ cpf: CPF.generate(true),
13
+ rg: '123',
14
+ birth_date: Date.today,
15
+ gender: 'f')
16
+ @role = Spree::Role.create(name: 'user')
17
+ @role.users << @user
18
+ sign_in_as! @user
19
+ end
20
+
21
+ it 'should show the brazilian fields in users#show', js: true do
22
+ visit spree.account_path
23
+
24
+ expect(page).to have_text @user.name
25
+ expect(page).to have_text @user.cpf
26
+ expect(page).to have_text @user.rg
27
+ expect(page).to have_text @user.birth_date.strftime(I18n.t('date.formats.default'))
28
+ expect(page).to have_text 'Female'
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'General Settings', type: :feature do
4
+
5
+ # login into admin page
6
+ before do
7
+ @admin = Spree.user_class.create(email: 'admin@admin.com', password: 'password', password_confirmation: 'password')
8
+ @role = Spree::Role.create(name: 'admin')
9
+ @role.users << @admin
10
+ sign_in_admin! @admin
11
+ end
12
+
13
+ context 'user registration settings' do
14
+ it 'should show the attribute sms permission', js: true do
15
+ visit spree.edit_admin_general_settings_path
16
+
17
+ expect(page).to have_text Spree.t(:user_registration_settings)
18
+ expect(page).to have_selector '#sms_permission'
19
+ end
20
+
21
+ it 'should possible check/uncheck the sms permission', js: true do
22
+ visit spree.edit_admin_general_settings_path
23
+
24
+ find(:css, '#sms_permission').set false
25
+
26
+ click_button 'Update'
27
+
28
+ expect(Spree::Config.sms_permission).to be(false)
29
+ expect(find_field('sms_permission')).not_to be_checked
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,150 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Users', type: :feature do
4
+
5
+ # login into admin page
6
+ before do
7
+ @admin = Spree.user_class.create(email: 'admin@admin.com', password: 'password', password_confirmation: 'password')
8
+ @role = Spree::Role.create(name: 'admin')
9
+ @role.users << @admin
10
+ sign_in_admin! @admin
11
+ end
12
+
13
+ context 'adding brazilian fields to users form' do
14
+
15
+ it 'should show common brazilian fields in users form', js: true do
16
+ Spree::Config.sms_permission = true
17
+
18
+ visit spree.new_admin_user_path
19
+
20
+ expect(page).to have_selector '#user_account_type_personal'
21
+ expect(page).to have_selector '#user_account_type_business'
22
+ expect(page).to have_selector '#user_newsletter'
23
+ expect(page).to have_selector '#user_receive_sms'
24
+ end
25
+
26
+ context 'disable sms permission' do
27
+ it 'should hide if the sms permission is false' do
28
+ Spree::Config.sms_permission = false
29
+
30
+ visit spree.new_admin_user_path
31
+
32
+ expect(page).not_to have_selector '#user_receive_sms'
33
+
34
+ # set default
35
+ Spree::Config.sms_permission = true
36
+ end
37
+ end
38
+
39
+ context 'choosing account type' do
40
+ it 'should show only personal account fields', js: true do
41
+ visit spree.new_admin_user_path
42
+
43
+ find(:css, '#user_account_type_personal').set true
44
+
45
+ expect(page).to have_text Spree.t(:name).upcase
46
+ expect(page).to have_selector '#user_cpf'
47
+ expect(page).to have_selector '#user_rg'
48
+ expect(page).to have_selector '#user_birth_date'
49
+ expect(page).to have_selector '#user_gender_f'
50
+ expect(page).to have_selector '#user_gender_m'
51
+
52
+ expect(page).not_to have_selector '#user_cnpj'
53
+ expect(page).not_to have_selector '#user_state_registry'
54
+ expect(page).not_to have_selector '#user_contact'
55
+ end
56
+
57
+ it 'should show only business account fields', js: true do
58
+ visit spree.new_admin_user_path
59
+
60
+ find(:css, '#user_account_type_business').set true
61
+
62
+ expect(page).to have_text Spree.t(:corporate_name).upcase
63
+ expect(page).to have_selector '#user_cnpj'
64
+ expect(page).to have_selector '#user_state_registry'
65
+ expect(page).to have_selector '#user_contact'
66
+
67
+ expect(page).not_to have_selector '#user_cpf'
68
+ expect(page).not_to have_selector '#user_rg'
69
+ expect(page).not_to have_selector '#user_birth_date'
70
+ expect(page).not_to have_selector '#user_gender_f'
71
+ expect(page).not_to have_selector '#user_gender_m'
72
+ end
73
+ end
74
+
75
+ context 'saving brazilian fields' do
76
+ context 'personal account' do
77
+ it 'can edit the personal account', js: true do
78
+ # gera um cnpj valido
79
+ cpf = CPF.generate true
80
+
81
+ Spree::Config.sms_permission = true
82
+
83
+ visit spree.edit_admin_user_path @admin.id
84
+
85
+ find(:css, '#user_account_type_personal').set true
86
+
87
+ fill_in 'user_name', with: 'users name'
88
+ find(:css, '#user_newsletter').set true
89
+ find(:css, '#user_receive_sms').set true
90
+ # preenche o campo cpf por js
91
+ # por causa da mascara
92
+ page.execute_script "$('#user_cpf').val('#{cpf}');"
93
+ fill_in 'user_rg', with: '123'
94
+ fill_in 'user_birth_date', with: '1990/01/01'
95
+ find(:css, '#user_gender_m').set true
96
+ click_button 'Update'
97
+
98
+ # verificando o objeto
99
+ @admin.reload
100
+ expect(@admin.name).to eq('users name')
101
+ expect(@admin.cpf).to eq cpf
102
+ expect(@admin.rg).to eq '123'
103
+ expect(@admin.birth_date).to eq Date.parse('1990/01/01')
104
+ expect(@admin.gender).to eq 'm'
105
+ expect(@admin.receive_sms).to eq 1
106
+ expect(@admin.newsletter).to eq 1
107
+ # verificando o formulario
108
+ expect(find_field('user_name').value).to eq 'users name'
109
+ expect(find_field('user_cpf').value).to eq cpf
110
+ expect(find_field('user_rg').value).to eq '123'
111
+ expect(find_field('user_birth_date').value).to eq '1990/01/01'
112
+ expect(find_field('user_gender_m')).to be_checked
113
+ expect(find_field('user_newsletter')).to be_checked
114
+ expect(find_field('user_receive_sms')).to be_checked
115
+ end
116
+ end
117
+
118
+ context 'business account' do
119
+ it 'can edit the business account', js: true do
120
+ # gera um cnpj valido
121
+ cnpj = CNPJ.generate true
122
+
123
+ visit spree.edit_admin_user_path @admin.id
124
+
125
+ find(:css, '#user_account_type_business').set true
126
+
127
+ fill_in 'user_name', with: 'users name'
128
+ # preenche o campo cnpj por js
129
+ # por causa da mascara
130
+ page.execute_script "$('#user_cnpj').val('#{cnpj}');"
131
+ fill_in 'user_state_registry', with: '123'
132
+ fill_in 'user_contact', with: 'company contact'
133
+ click_button 'Update'
134
+
135
+ # verificando o objeto
136
+ @admin.reload
137
+ expect(@admin.name).to eq 'users name'
138
+ expect(@admin.cnpj).to eq cnpj
139
+ expect(@admin.state_registry).to eq '123'
140
+ expect(@admin.contact).to eq 'company contact'
141
+ # verificando o formulario
142
+ expect(find_field('user_name').value).to eq 'users name'
143
+ expect(find_field('user_cnpj').value).to eq cnpj
144
+ expect(find_field('user_state_registry').value).to eq '123'
145
+ expect(find_field('user_contact').value).to eq 'company contact'
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Sign Up', type: :feature do
4
+ context 'brazilian fields' do
5
+
6
+ it 'should show common brazilian fields', js: true do
7
+ # set sms permission
8
+ Spree::Config.sms_permission = true
9
+
10
+ visit spree.signup_path
11
+
12
+ expect(page).to have_selector '#spree_user_account_type_personal'
13
+ expect(page).to have_selector '#spree_user_account_type_business'
14
+ expect(page).to have_selector '#spree_user_newsletter'
15
+ expect(page).to have_selector '#spree_user_receive_sms'
16
+ end
17
+
18
+ context 'disable sms permission' do
19
+ it 'should hide if the sms permission is false', js: true do
20
+ Spree::Config.sms_permission = false
21
+
22
+ visit spree.signup_path
23
+
24
+ expect(page).not_to have_selector '#spree_user_receive_sms'
25
+
26
+ # set default
27
+ Spree::Config.sms_permission = true
28
+ end
29
+ end
30
+
31
+ context 'choosing account type' do
32
+ it 'should show only personal account fields', js: true do
33
+ visit spree.signup_path
34
+
35
+ find(:css, '#spree_user_account_type_personal').set true
36
+
37
+ expect(page).to have_selector '#spree_user_cpf'
38
+ expect(page).to have_selector '#spree_user_rg'
39
+ expect(page).to have_selector '#spree_user_birth_date'
40
+ expect(page).to have_selector '#spree_user_gender_f'
41
+ expect(page).to have_selector '#spree_user_gender_m'
42
+
43
+ expect(page).not_to have_selector '#spree_user_cnpj'
44
+ expect(page).not_to have_selector '#spree_user_state_registry'
45
+ expect(page).not_to have_selector '#spree_user_contact'
46
+ end
47
+
48
+ it 'should show only business account fields', js: true do
49
+ visit spree.signup_path
50
+
51
+ find(:css, '#spree_user_account_type_business').set true
52
+
53
+ expect(page).to have_selector '#spree_user_cnpj'
54
+ expect(page).to have_selector '#spree_user_state_registry'
55
+ expect(page).to have_selector '#spree_user_contact'
56
+
57
+ expect(page).not_to have_selector '#spree_user_cpf'
58
+ expect(page).not_to have_selector '#spree_user_rg'
59
+ expect(page).not_to have_selector '#spree_user_birth_date'
60
+ expect(page).not_to have_selector '#spree_user_gender_f'
61
+ expect(page).not_to have_selector '#spree_user_gender_m'
62
+ end
63
+ end
64
+
65
+ context 'creating a new user' do
66
+ it 'should create a new user', js: true do
67
+ # gera um cnpj valido
68
+ cnpj = CNPJ.generate true
69
+
70
+ visit spree.signup_path
71
+
72
+ find(:css, '#spree_user_account_type_business').set true
73
+
74
+ fill_in 'Corporate Name', with: 'some name'
75
+ fill_in 'Email', with: 'email@person.com'
76
+ fill_in 'Password', with: 'password'
77
+ fill_in 'Password Confirmation', with: 'password'
78
+ # preenche o campo cnpj por js
79
+ # por causa da mascara
80
+ page.execute_script '$("#spree_user_cnpj").val("' + cnpj + '");'
81
+ fill_in 'State registry', with: '123'
82
+ fill_in 'Contact', with: 'contact'
83
+ click_button 'Create'
84
+
85
+ new_user = Spree::User.last
86
+
87
+ expect(new_user.business_account?).to be(true)
88
+ expect(new_user.cnpj).to eq(cnpj)
89
+ expect(new_user.state_registry).to eq('123')
90
+ expect(new_user.contact).to eq('contact')
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::AppConfiguration, type: :model do
4
+
5
+ it 'attribute sms permission should exist' do
6
+ expect(Spree::Config.has_preference?(:sms_permission)).to be true
7
+ end
8
+
9
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::User do
4
+
5
+ context 'validate brazilian fields' do
6
+
7
+ let(:user) { FactoryGirl.build(:user) }
8
+
9
+ it 'should be invalid if the account type has a invalid value' do
10
+ # values for account type:
11
+ # personal or business
12
+ user.account_type = ''
13
+ expect(user.valid?).to be(false)
14
+ end
15
+
16
+ it 'should be invalid if cpf is invalid' do
17
+ user.cpf = '1234'
18
+ expect(user.valid?).to be false
19
+ end
20
+
21
+ it 'should be invalid if the user has the same CPF of other user' do
22
+ # gera um cnpj valido
23
+ cpf = CPF.generate true
24
+
25
+ user.cpf = cpf
26
+ user.save
27
+
28
+ new_user = FactoryGirl.build(:user)
29
+ new_user.cpf = cpf
30
+
31
+ expect(new_user.valid?).to be(false)
32
+ end
33
+
34
+ it 'should be invalid if cnpj is invalid' do
35
+ user.cnpj = '1234'
36
+ expect(user.valid?).to be false
37
+ end
38
+
39
+ it 'should be invalid if the user has the same CNPJ of other user' do
40
+ # gera um cnpj valido
41
+ cnpj = CNPJ.generate true
42
+
43
+ user.cnpj = cnpj
44
+ user.save
45
+
46
+ new_user = FactoryGirl.build(:user)
47
+ new_user.cnpj = cnpj
48
+
49
+ expect(new_user.valid?).to be(false)
50
+ end
51
+
52
+ context 'personal or business account' do
53
+ let(:user) { Spree::User.new }
54
+
55
+ it 'the default account type should be personal' do
56
+ expect(user.account_type).to eq('personal')
57
+ end
58
+
59
+ it 'verify if the user has a personal account' do
60
+ expect(user.personal_account?).to be(true)
61
+ end
62
+
63
+ it 'verify if the user has a business account' do
64
+ user.account_type = 'business'
65
+ expect(user.business_account?).to be(true)
66
+ end
67
+ end
68
+
69
+ context 'document' do
70
+ let(:user) { Spree::User.new }
71
+
72
+ it 'should return the CPF in method document if personal account' do
73
+ user.cpf = CPF.generate true
74
+ expect(user.document).to eq user.cpf
75
+ end
76
+
77
+ it 'should return the CNPJ in method document if business account' do
78
+ user.account_type = 'business'
79
+ user.cnpj = CNPJ.generate true
80
+ expect(user.document).to eq user.cnpj
81
+ end
82
+ end
83
+
84
+ context 'overriding attributes methods' do
85
+ let(:user) { Spree::User.new }
86
+
87
+ it 'should return document in the list of attribute names' do
88
+ expect(user.attribute_names.include?('document')).to be true
89
+ end
90
+
91
+ it 'should return the document in method attributes' do
92
+ user.cpf = CPF.generate true
93
+ expect(user.attributes['document']).to eq user.document
94
+ end
95
+ end
96
+ end
97
+ end