eac_rails_utils 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/Rakefile +18 -0
  3. data/lib/assets/javascripts/currency_field.js +21 -0
  4. data/lib/assets/javascripts/eac_rails_utils.js +6 -0
  5. data/lib/assets/javascripts/input_searchable.js +78 -0
  6. data/lib/assets/javascripts/jMenu.jquery.min.js +12 -0
  7. data/lib/assets/javascripts/jquery.maskMoney.js +409 -0
  8. data/lib/assets/stylesheets/autocomplete.scss +33 -0
  9. data/lib/assets/stylesheets/bootstrap3-dropdown.scss +47 -0
  10. data/lib/assets/stylesheets/eac_rails_utils.scss +44 -0
  11. data/lib/assets/stylesheets/jmenu.scss +78 -0
  12. data/lib/eac/common_form_helper.rb +37 -0
  13. data/lib/eac/common_form_helper/form_builder.rb +88 -0
  14. data/lib/eac/common_form_helper/form_builder/association_select_field.rb +40 -0
  15. data/lib/eac/common_form_helper/form_builder/common_text_fields.rb +18 -0
  16. data/lib/eac/common_form_helper/form_builder/currency_field.rb +39 -0
  17. data/lib/eac/common_form_helper/form_builder/date_field.rb +20 -0
  18. data/lib/eac/common_form_helper/form_builder/fields_for.rb +44 -0
  19. data/lib/eac/common_form_helper/form_builder/radio_select_field.rb +44 -0
  20. data/lib/eac/common_form_helper/form_builder/searchable_association_field.rb +77 -0
  21. data/lib/eac/cpf_validator.rb +60 -0
  22. data/lib/eac/formatter_helper.rb +27 -0
  23. data/lib/eac/htmlbeautifier.rb +25 -0
  24. data/lib/eac/menus_helper.rb +17 -0
  25. data/lib/eac/menus_helper/bootstrap_gui_builder.rb +90 -0
  26. data/lib/eac/menus_helper/data_builder.rb +57 -0
  27. data/lib/eac/menus_helper/gui_builder.rb +51 -0
  28. data/lib/eac/model.rb +87 -0
  29. data/lib/eac/simple_cache.rb +29 -0
  30. data/lib/eac/source_target_fixtures.rb +51 -0
  31. data/lib/eac_rails_utils.rb +29 -0
  32. data/lib/eac_rails_utils/patches/model_attribute_required.rb +31 -0
  33. data/lib/eac_rails_utils/rails/engine.rb +5 -0
  34. data/lib/eac_rails_utils/version.rb +3 -0
  35. data/test/dummy/Rakefile +6 -0
  36. data/test/dummy/app/models/job.rb +4 -0
  37. data/test/dummy/app/models/user.rb +7 -0
  38. data/test/dummy/config.ru +4 -0
  39. data/test/dummy/config/application.rb +12 -0
  40. data/test/dummy/config/boot.rb +4 -0
  41. data/test/dummy/config/database.yml +5 -0
  42. data/test/dummy/config/environment.rb +5 -0
  43. data/test/dummy/config/environments/test.rb +42 -0
  44. data/test/dummy/config/routes.rb +8 -0
  45. data/test/dummy/config/secrets.yml +22 -0
  46. data/test/dummy/db/migrate/20160415125333_create_users.rb +9 -0
  47. data/test/dummy/db/migrate/20160415143123_create_jobs.rb +7 -0
  48. data/test/dummy/db/migrate/20160415143229_add_job_to_users.rb +5 -0
  49. data/test/dummy/db/schema.rb +25 -0
  50. data/test/lib/eac/common_form_helper_test.rb +31 -0
  51. data/test/lib/eac/cpf_validator_test.rb +33 -0
  52. data/test/lib/eac/formatter_helper_test.rb +19 -0
  53. data/test/lib/eac/model_test.rb +76 -0
  54. data/test/lib/eac/simple_cache_test.rb +56 -0
  55. data/test/lib/eac_rails_utils/patches/model_attribute_required_test.rb +39 -0
  56. data/test/test_helper.rb +11 -0
  57. metadata +189 -0
@@ -0,0 +1,7 @@
1
+ class CreateJobs < ActiveRecord::Migration
2
+ def change
3
+ create_table :jobs do |t|
4
+ t.string :name
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ class AddJobToUsers < ActiveRecord::Migration
2
+ def change
3
+ add_belongs_to :users, :job
4
+ end
5
+ end
@@ -0,0 +1,25 @@
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 that you check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(version: 20_160_415_143_229) do
15
+ create_table 'jobs', force: :cascade do |t|
16
+ t.string 'name'
17
+ end
18
+
19
+ create_table 'users', force: :cascade do |t|
20
+ t.string 'name'
21
+ t.string 'email'
22
+ t.string 'password'
23
+ t.integer 'job_id'
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ module Eac
4
+ class CommonFormHelperTest < ActionView::TestCase
5
+ include Eac::CommonFormHelper
6
+
7
+ setup do
8
+ reset_test_database
9
+ end
10
+
11
+ test 'test common form' do
12
+ common_form(User.new) do |f|
13
+ f.text_field :name
14
+ f.email_field :email
15
+ f.password_field :password
16
+ f.searchable_association_field :job, url: search_jobs_path
17
+ end
18
+ end
19
+
20
+ test 'fields for' do
21
+ common_form(Job.new) do |f|
22
+ f.text_field :name
23
+ f.fields_for :users do |nf|
24
+ nf.text_field :name
25
+ nf.email_field :email
26
+ nf.password_field :password
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,33 @@
1
+ require 'test_helper'
2
+
3
+ module Eac
4
+ class CpfValidatorTest < ActiveSupport::TestCase
5
+ class M1
6
+ include ActiveModel::Model
7
+
8
+ attr_accessor :cpf
9
+
10
+ validates :cpf, 'eac/cpf' => true, allow_nil: true
11
+ end
12
+
13
+ def setup
14
+ @record = M1.new
15
+ end
16
+
17
+ def test_valid_cpfs
18
+ ['85630275305', '66244374487', nil].each do |v|
19
+ @record.cpf = v
20
+ @record.valid?
21
+ assert_equal [], @record.errors[:cpf], "CPF: \"#{v}\""
22
+ end
23
+ end
24
+
25
+ def test_invalid_cpfs
26
+ ['', ' ', 'abc', '856.302.753-05', '662.443.744-87', '85630275304'].each do |v|
27
+ @record.cpf = v
28
+ @record.valid?
29
+ assert_not_equal [], @record.errors[:cpf], "CPF: \"#{v}\""
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ module Eac
4
+ class FormatterHelperTest < ActionView::TestCase
5
+ include Eac::FormatterHelper
6
+
7
+ test 'should convert BRL currency to float' do
8
+ brl_currency = { a: '1', b: '1,2', c: '1,23', d: '123.456.789,01',
9
+ e: 'R$1,23', f: 'R$ 123.4,56' }
10
+ float_currency = { a: 1, b: 1.2, c: 1.23, d: 123_456_789.01,
11
+ e: 1.23, f: 123_4.56 }
12
+
13
+ brl_currency.each do |k, v|
14
+ assert_equal float_currency[k], brl_currency_to_float(v),
15
+ "#{v} should be #{float_currency[k]}"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,76 @@
1
+ require 'test_helper'
2
+
3
+ module Eac
4
+ class ModelTest < ActiveSupport::TestCase
5
+ class M1
6
+ include ActiveModel::Model
7
+ include Eac::Model
8
+ attr_accessor :name, :age, :account, :country_id, :other
9
+ end
10
+
11
+ class M2
12
+ include ActiveModel::Model
13
+ attr_accessor :name, :myage, :account_id, :country
14
+
15
+ validate :my_validate
16
+
17
+ def my_validate
18
+ errors.add(:name, 'NAME_ERROR')
19
+ errors.add(:myage, 'MYAGE_ERROR')
20
+ errors.add(:account_id, 'ACCOUNT_ID_ERROR')
21
+ errors.add(:country, 'COUNTRY_ERROR')
22
+ end
23
+ end
24
+
25
+ def setup
26
+ reset_m1
27
+ reset_m2
28
+ end
29
+
30
+ def test_fetch_column_errors
31
+ @m1.fetch_column_errors(@m2, :myage, :age)
32
+ assert_equal @m2.errors[:myage], @m1.errors[:age]
33
+ end
34
+
35
+ def test_fetch_record_errors
36
+ @m1.fetch_record_errors(@m2)
37
+ { name: :name, account_id: :account, country: :country_id }.each do |c2, c1|
38
+ assert_equal @m2.errors[c2], @m1.errors[c1], "c2: #{c2}, c1: #{c1}"
39
+ end
40
+ assert @m1.errors[:age].empty?
41
+ end
42
+
43
+ def test_fetch_record_errors_with_default_column
44
+ @m1.fetch_record_errors(@m2, default_column: :other)
45
+ assert_equal @m2.errors[:name], @m1.errors[:name]
46
+ assert @m1.errors[:age].empty?
47
+ assert_equal ['Myage: MYAGE_ERROR'], @m1.errors[:other]
48
+ end
49
+
50
+ def test_fetch_record_errors_with_skip_option
51
+ @m1.fetch_record_errors(@m2, skip: [:name])
52
+ assert @m1.errors[:name].empty?
53
+ assert @m1.errors[:age].empty?
54
+ end
55
+
56
+ def test_fetch_record_errors_by_mapping
57
+ @m1.fetch_record_errors_by_mapping(@m2, name: :name, myage: :age)
58
+ assert_equal @m2.errors[:name], @m1.errors[:name]
59
+ assert_equal @m2.errors[:myage], @m1.errors[:age]
60
+ end
61
+
62
+ private
63
+
64
+ def reset_m1
65
+ @m1 = M1.new
66
+ assert @m1.errors.empty?
67
+ end
68
+
69
+ def reset_m2
70
+ @m2 = M2.new
71
+ assert_not @m2.valid?
72
+ assert_not @m2.errors[:name].empty?
73
+ assert_not @m2.errors[:myage].empty?
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,56 @@
1
+ require 'test_helper'
2
+
3
+ module Eac
4
+ class SimpleCacheTest < ActionView::TestCase
5
+ class CacheableObject
6
+ include Eac::SimpleCache
7
+
8
+ def my_method_uncached
9
+ @counter ||= 0
10
+ @counter += 1
11
+ end
12
+
13
+ def method_with_args_uncached(arg1)
14
+ @counter2 ||= 0
15
+ @counter2 += 1
16
+ "#{arg1}/#{@counter2}"
17
+ end
18
+
19
+ private
20
+
21
+ def private_method_uncached
22
+ @counter3 ||= 0
23
+ @counter3 += 1
24
+ end
25
+ end
26
+
27
+ def setup
28
+ @co = CacheableObject.new
29
+ end
30
+
31
+ def test_cached_value
32
+ assert_not_equal @co.my_method_uncached, @co.my_method_uncached
33
+ assert_equal @co.my_method, @co.my_method
34
+ end
35
+
36
+ def test_cached_value_with_args
37
+ assert_not_equal @co.method_with_args_uncached('123'), @co.method_with_args_uncached('123')
38
+ assert_not_equal @co.method_with_args_uncached('456'), @co.method_with_args_uncached('456')
39
+ assert_equal @co.method_with_args('123'), @co.method_with_args('123')
40
+ assert_equal @co.method_with_args('456'), @co.method_with_args('456')
41
+ assert_not_equal @co.method_with_args('123'), @co.method_with_args('456')
42
+ end
43
+
44
+ def test_private_uncached_method
45
+ assert_equal @co.private_method, @co.private_method
46
+ end
47
+
48
+ def test_reset
49
+ @co = CacheableObject.new
50
+ value = @co.my_method
51
+ assert_equal value, @co.my_method
52
+ @co.reset_cache
53
+ assert_not_equal value, @co.my_method
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ module EacRailsUtils
4
+ module Patches
5
+ class ModelAttributeRequiredTest < ActiveSupport::TestCase
6
+ setup do
7
+ reset_test_database
8
+ end
9
+
10
+ test 'column with presence validator should be required' do
11
+ assert User.column_required?(:password)
12
+ end
13
+
14
+ test 'column with format validator should be required' do
15
+ assert User.column_required?(:email)
16
+ end
17
+
18
+ test 'column without validators should be optional' do
19
+ assert_not User.column_required?(:name)
20
+ end
21
+
22
+ test 'association with presence validator should be required' do
23
+ assert User.column_required?(:job)
24
+ end
25
+
26
+ test 'required column in active model' do
27
+ assert ActiveModelStub.column_required?(:name), 'name is required'
28
+ assert_not ActiveModelStub.column_required?(:age), 'age is optional'
29
+ end
30
+
31
+ class ActiveModelStub
32
+ include ActiveModel::Model
33
+
34
+ attr_accessor :name, :age
35
+ validates :name, presence: true
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,11 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+ require File.expand_path('../../test/dummy/config/environment.rb', __FILE__)
3
+ require 'rails/test_help'
4
+
5
+ module ActiveSupport
6
+ class TestCase
7
+ def reset_test_database
8
+ ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV['SCHEMA'])
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eac_rails_utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - E.A.C.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: htmlbeautifier
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.1.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bootstrap-sass
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.3.6
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.3.6
55
+ - !ruby/object:Gem::Dependency
56
+ name: nested_form_fields
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - Rakefile
90
+ - lib/assets/javascripts/currency_field.js
91
+ - lib/assets/javascripts/eac_rails_utils.js
92
+ - lib/assets/javascripts/input_searchable.js
93
+ - lib/assets/javascripts/jMenu.jquery.min.js
94
+ - lib/assets/javascripts/jquery.maskMoney.js
95
+ - lib/assets/stylesheets/autocomplete.scss
96
+ - lib/assets/stylesheets/bootstrap3-dropdown.scss
97
+ - lib/assets/stylesheets/eac_rails_utils.scss
98
+ - lib/assets/stylesheets/jmenu.scss
99
+ - lib/eac/common_form_helper.rb
100
+ - lib/eac/common_form_helper/form_builder.rb
101
+ - lib/eac/common_form_helper/form_builder/association_select_field.rb
102
+ - lib/eac/common_form_helper/form_builder/common_text_fields.rb
103
+ - lib/eac/common_form_helper/form_builder/currency_field.rb
104
+ - lib/eac/common_form_helper/form_builder/date_field.rb
105
+ - lib/eac/common_form_helper/form_builder/fields_for.rb
106
+ - lib/eac/common_form_helper/form_builder/radio_select_field.rb
107
+ - lib/eac/common_form_helper/form_builder/searchable_association_field.rb
108
+ - lib/eac/cpf_validator.rb
109
+ - lib/eac/formatter_helper.rb
110
+ - lib/eac/htmlbeautifier.rb
111
+ - lib/eac/menus_helper.rb
112
+ - lib/eac/menus_helper/bootstrap_gui_builder.rb
113
+ - lib/eac/menus_helper/data_builder.rb
114
+ - lib/eac/menus_helper/gui_builder.rb
115
+ - lib/eac/model.rb
116
+ - lib/eac/simple_cache.rb
117
+ - lib/eac/source_target_fixtures.rb
118
+ - lib/eac_rails_utils.rb
119
+ - lib/eac_rails_utils/patches/model_attribute_required.rb
120
+ - lib/eac_rails_utils/rails/engine.rb
121
+ - lib/eac_rails_utils/version.rb
122
+ - test/dummy/Rakefile
123
+ - test/dummy/app/models/job.rb
124
+ - test/dummy/app/models/user.rb
125
+ - test/dummy/config.ru
126
+ - test/dummy/config/application.rb
127
+ - test/dummy/config/boot.rb
128
+ - test/dummy/config/database.yml
129
+ - test/dummy/config/environment.rb
130
+ - test/dummy/config/environments/test.rb
131
+ - test/dummy/config/routes.rb
132
+ - test/dummy/config/secrets.yml
133
+ - test/dummy/db/migrate/20160415125333_create_users.rb
134
+ - test/dummy/db/migrate/20160415143123_create_jobs.rb
135
+ - test/dummy/db/migrate/20160415143229_add_job_to_users.rb
136
+ - test/dummy/db/schema.rb
137
+ - test/lib/eac/common_form_helper_test.rb
138
+ - test/lib/eac/cpf_validator_test.rb
139
+ - test/lib/eac/formatter_helper_test.rb
140
+ - test/lib/eac/model_test.rb
141
+ - test/lib/eac/simple_cache_test.rb
142
+ - test/lib/eac_rails_utils/patches/model_attribute_required_test.rb
143
+ - test/test_helper.rb
144
+ homepage:
145
+ licenses: []
146
+ metadata: {}
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubyforge_project:
163
+ rubygems_version: 2.4.8
164
+ signing_key:
165
+ specification_version: 4
166
+ summary: Código reutilizável para as aplicações Rails da E.A.C..
167
+ test_files:
168
+ - test/lib/eac_rails_utils/patches/model_attribute_required_test.rb
169
+ - test/lib/eac/formatter_helper_test.rb
170
+ - test/lib/eac/common_form_helper_test.rb
171
+ - test/lib/eac/cpf_validator_test.rb
172
+ - test/lib/eac/model_test.rb
173
+ - test/lib/eac/simple_cache_test.rb
174
+ - test/dummy/config.ru
175
+ - test/dummy/db/migrate/20160415143229_add_job_to_users.rb
176
+ - test/dummy/db/migrate/20160415143123_create_jobs.rb
177
+ - test/dummy/db/migrate/20160415125333_create_users.rb
178
+ - test/dummy/db/schema.rb
179
+ - test/dummy/app/models/user.rb
180
+ - test/dummy/app/models/job.rb
181
+ - test/dummy/Rakefile
182
+ - test/dummy/config/environments/test.rb
183
+ - test/dummy/config/application.rb
184
+ - test/dummy/config/boot.rb
185
+ - test/dummy/config/secrets.yml
186
+ - test/dummy/config/database.yml
187
+ - test/dummy/config/routes.rb
188
+ - test/dummy/config/environment.rb
189
+ - test/test_helper.rb