support_utils 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +243 -0
  4. data/Rakefile +43 -0
  5. data/app/assets/javascripts/support_utils/application.js +13 -0
  6. data/app/assets/stylesheets/support_utils/application.css +15 -0
  7. data/app/controllers/support_utils/application_controller.rb +4 -0
  8. data/app/helpers/support_utils/application_helper.rb +4 -0
  9. data/app/views/layouts/support_utils/application.html.erb +14 -0
  10. data/config/routes.rb +2 -0
  11. data/lib/support_utils/all.rb +5 -0
  12. data/lib/support_utils/concerns/core.rb +19 -0
  13. data/lib/support_utils/concerns/utils.rb +139 -0
  14. data/lib/support_utils/core/active_record/utils.rb +30 -0
  15. data/lib/support_utils/engine.rb +17 -0
  16. data/lib/support_utils/version.rb +3 -0
  17. data/lib/support_utils.rb +5 -0
  18. data/lib/tasks/support_utils_tasks.rake +4 -0
  19. data/test/dummy/README.rdoc +28 -0
  20. data/test/dummy/Rakefile +6 -0
  21. data/test/dummy/app/assets/javascripts/application.js +13 -0
  22. data/test/dummy/app/assets/javascripts/home.js +2 -0
  23. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  24. data/test/dummy/app/assets/stylesheets/home.css +4 -0
  25. data/test/dummy/app/controllers/application_controller.rb +5 -0
  26. data/test/dummy/app/controllers/home_controller.rb +16 -0
  27. data/test/dummy/app/helpers/application_helper.rb +2 -0
  28. data/test/dummy/app/helpers/home_helper.rb +2 -0
  29. data/test/dummy/app/models/order.rb +4 -0
  30. data/test/dummy/app/models/user.rb +5 -0
  31. data/test/dummy/app/views/home/index.html.haml +14 -0
  32. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  33. data/test/dummy/bin/bundle +3 -0
  34. data/test/dummy/bin/rails +4 -0
  35. data/test/dummy/bin/rake +4 -0
  36. data/test/dummy/bin/setup +29 -0
  37. data/test/dummy/config/application.rb +25 -0
  38. data/test/dummy/config/boot.rb +5 -0
  39. data/test/dummy/config/database.yml +25 -0
  40. data/test/dummy/config/environment.rb +5 -0
  41. data/test/dummy/config/environments/development.rb +41 -0
  42. data/test/dummy/config/environments/production.rb +79 -0
  43. data/test/dummy/config/environments/test.rb +42 -0
  44. data/test/dummy/config/initializers/assets.rb +11 -0
  45. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  46. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  47. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  48. data/test/dummy/config/initializers/inflections.rb +16 -0
  49. data/test/dummy/config/initializers/mime_types.rb +4 -0
  50. data/test/dummy/config/initializers/session_store.rb +3 -0
  51. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  52. data/test/dummy/config/locales/en.yml +23 -0
  53. data/test/dummy/config/routes.rb +6 -0
  54. data/test/dummy/config/secrets.yml +22 -0
  55. data/test/dummy/config.ru +4 -0
  56. data/test/dummy/db/development.sqlite3 +0 -0
  57. data/test/dummy/db/migrate/20150421030302_create_users.rb +15 -0
  58. data/test/dummy/db/migrate/20150424032916_create_orders.rb +13 -0
  59. data/test/dummy/db/schema.rb +40 -0
  60. data/test/dummy/db/seeds.rb +22 -0
  61. data/test/dummy/db/test.sqlite3 +0 -0
  62. data/test/dummy/log/development.log +21189 -0
  63. data/test/dummy/log/test.log +24976 -0
  64. data/test/dummy/public/404.html +67 -0
  65. data/test/dummy/public/422.html +67 -0
  66. data/test/dummy/public/500.html +66 -0
  67. data/test/dummy/public/favicon.ico +0 -0
  68. data/test/dummy/test/controllers/home_controller_test.rb +9 -0
  69. data/test/dummy/test/fixtures/orders.yml +11 -0
  70. data/test/dummy/test/fixtures/users.yml +11 -0
  71. data/test/dummy/test/models/order_test.rb +7 -0
  72. data/test/dummy/test/models/user_test.rb +7 -0
  73. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/3NRucTfJ9BKkkU15VHmiAFLNCBXUcM7cI0lC7kfC9cs.cache +1 -0
  74. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/3znTKTngGxqbpsBO_atx9ttmvCC2R3F5bTTs_c2ZwlQ.cache +2 -0
  75. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/7iGVpTK8ipk4icjxlsN7_axlCbiz8VLUey6zaDm5CB4.cache +1 -0
  76. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/94Td0gD2ZdGZXjN-lqW_GPcwPnMzVVC8YOdC3murnNw.cache +1 -0
  77. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/AbJ-9O9KeXeOV2JPNKgKAu1cshodEuc1fr1d6nSW1Tw.cache +0 -0
  78. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/BakjJOfl62-U5OwnMPFaA5M6PxHCbIXUukbywsRc04I.cache +0 -0
  79. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/C43usClnARO4nvHgtvyMFUKTpQrCDh6QoILF0Sp4qtQ.cache +0 -0
  80. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/Cmv302frYnh4fgJ8_imPcZsOl7KXzTyG7D6TWCxWacw.cache +1 -0
  81. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/DGaYSlF6J66LSV7HU-CzM121_hmVW-ZckEuMH4dVGfE.cache +1 -0
  82. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/FhMdTeLemC2NktXS-qg79dcf-6XH0H6MubhtzrYwAHg.cache +0 -0
  83. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/T7YPma3x0I0a_j2-i-oKq62kCKhrERRH-keUpZj735U.cache +2 -0
  84. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/UMNBueOpK1ZOpvew838FGssDdE9cMBktro39Y0fyA1U.cache +0 -0
  85. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/WId-ddc3Lwxh3C2etcMTfFUeFcozMmvlqDyPG3mFZ_M.cache +1 -0
  86. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/Xr4odg5nk4lfGRnM6VyO8I0Tv1AFXSjMbS58IDdcmIQ.cache +1 -0
  87. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/_Z5XYP39T5mP_4LYCVvDRADi4FCIxb0NxKuexbsEUqk.cache +1 -0
  88. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/bSLZsG1luW7787CD0lmEKXV8Ilk34QWHjgIziRCLFVU.cache +2 -0
  89. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/e8zfeDl4Df2LqDkqLY-vidciI2Prs4VyI5z0Dl_bf5I.cache +0 -0
  90. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/eDF6xL-AIvprUjziaae0FZKKfuf17xuP6JpHyUXr9Es.cache +1 -0
  91. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/gbCssFRrBV6Nanj05aj9wAnwQWCquourp1Wt1xH7JeM.cache +0 -0
  92. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/h1k_cMvuG7MWjYeBIVsoCV2HvdnOLTEomuj-VF82O_U.cache +2 -0
  93. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/kDmo231uFksuDbjFiin7W3bXt0E6_tDkYerlfc54qb8.cache +2 -0
  94. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/lMSZWAnUxgeUMPFLbZ9mvwKP_JNxvAkfCeA7NKFNKnQ.cache +2 -0
  95. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/mwWZqSVVLbLYEwfPwboPPydlFFG03qIbXYNwrAkpKVo.cache +0 -0
  96. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/rx65z8GAlwmDS6WYI1x1XJ2Yl7aFGqFPhq746axBorw.cache +0 -0
  97. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/s7bYObbtJaDA2_hyGsE0aCJhVpPb92MvUrwbOwRWVCk.cache +1 -0
  98. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/vDs9JDRxJ8A5zQBufP3XVUaGeizn0gB8h_S1_99ixvc.cache +1 -0
  99. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/wQD8Ay8vR12nM1zd2gcdj3J4B5t2-YCN7EKfW_Scg2M.cache +0 -0
  100. data/test/dummy/tmp/cache/assets/development/sprockets/v3.0/xSrsaQkeSgOC3GH3MWTUsZuWMJNHKQlyEzZrATXjBCU.cache +2 -0
  101. data/test/integration/navigation_test.rb +10 -0
  102. data/test/support_utils_test.rb +7 -0
  103. data/test/test_helper.rb +20 -0
  104. metadata +348 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0aa086704ed53641c59928f9ec247ea97688b3ad
4
+ data.tar.gz: 0ac049d8aa70e95ccdbfc64c1f530b8014098771
5
+ SHA512:
6
+ metadata.gz: 6577e353ebb8485da58f9c8a0ae7bbd1e0499a8b02a7a48387a39631fc0d317be990acf36385b6405ab249b8a15282e1281643f5504fef0c29772bfcc18275d3
7
+ data.tar.gz: e7c53f57883658ba6ba5692a0c5ed80cc5b4677fdd2ca1e3c5dc9d3cbb3f0b1682299bdff51dfc969759b61c92a7543c9b6784531d7b2b3bb5e4a1395a516cd1
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Emilio Forrer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,243 @@
1
+ **SupportUtils**
2
+ ===================
3
+
4
+ SupportUtils is a collection of utility classes, helpers and standard library extensions that were found useful for develop projects, faster.
5
+
6
+ ----------
7
+
8
+
9
+ **Installation**
10
+ ------------
11
+
12
+
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```
17
+ gem 'support_utils'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ ```
23
+ bundle install
24
+ ```
25
+
26
+ Or install it yourself as:
27
+
28
+ ```
29
+ $ gem install support_utils
30
+ ```
31
+ <div style="page-break-after: always;"></div>
32
+
33
+ ##**Usage**
34
+
35
+ ### Active Record Models
36
+
37
+ To have available all the extra methods and helpers in your models, simply just call the `has_utils` method inside your model. Example:
38
+
39
+ ```
40
+ class Order < ActiveRecord::Base
41
+ has_utils
42
+ belongs_to :user
43
+ end
44
+
45
+ ```
46
+
47
+ ### List of methods added to your models
48
+
49
+ ### **`Model#column_name_format`**
50
+
51
+ This method behaves diferently depending on the type of the column in the database, here is the list behavior by type:
52
+
53
+ #### **`Model#column_name_format format, options`** - column type (Date, Datetime, etc)
54
+
55
+ When you call `Model#column_name_format` on a date column, it will use the `I18n.locale` and return the date formated.
56
+
57
+ List of arguments
58
+
59
+ * **format:** the format that is going to use to display the date. **Default value** **`:default`**.
60
+
61
+ Example using the completed_at attribute of the Order class.
62
+
63
+ ```
64
+ order = Order.first
65
+
66
+ order.completed_at
67
+ => Sat, 25 Apr 2015 18:29:40 UTC +00:00
68
+
69
+ # Default format
70
+
71
+ order.completed_at_format
72
+ => "Sat, 25 Apr 2015 18:29:40 +0000"
73
+
74
+ # Short format
75
+
76
+ order.completed_at_format :short
77
+ => "25 Apr 18:29"
78
+
79
+ ```
80
+
81
+
82
+
83
+ #### **`Model#column_name_format options`** - column type (Float, Decimal)
84
+
85
+
86
+ When you call `Model#column_name_format` on a decimal column, it will use some number helpers (such as `number_with_precision` and `number_to_currency`) to display the value.
87
+
88
+ List of options
89
+
90
+ * **currency:** if set to true it will call `number_to_currency` to display the value.
91
+
92
+ Example using the total attribute of the Order class.
93
+
94
+ ```
95
+ order = Order.first
96
+
97
+ order.total.to_s
98
+ => "29.15586957"
99
+
100
+ # Default
101
+
102
+ order.total_format
103
+ => "29.16"
104
+
105
+ # Currency
106
+
107
+ order.total_format currency: true
108
+ => "$29.16"
109
+
110
+ # Currency diferent unit
111
+
112
+ order.total_format currency: true, unit: "€"
113
+ => "€29.16"
114
+
115
+
116
+ ```
117
+
118
+ <div style="page-break-after: always;"></div>
119
+
120
+ #### **`Model#column_name_format options`** - column type (String, Text)
121
+
122
+
123
+ When you call `Model#column_name_format` on a string column, it will use some text helpers (such as `simple_format` and `truncate`) to display the value. By default uses `simple_format`.
124
+
125
+ List of options
126
+
127
+ * **truncate:** Truncates a given text after a given :length if text is longer than :length (defaults to 30). The last characters will be replaced with the :omission (defaults to “…”) for a total length not exceeding :length.
128
+ * **word_wrap:** Wraps the text into lines no longer than line_width width. This method breaks on the first whitespace character that does not exceed line_width (which is 80 by default).
129
+ * **excerpt:** Extracts an excerpt from text that matches the first instance of phrase.
130
+
131
+
132
+ Example using the description attribute of the Order class.
133
+
134
+
135
+ ```
136
+ order = Order.first
137
+
138
+ order.description
139
+ => "Excepturi eum eius sed quia sint fuga veritatis. Nihil nisi omnis ullam voluptatem doloremque iure. Rerum eum recusandae est sit qui odio eum. Et laboriosam voluptas. Beatae qui voluptates velit est soluta. Fugiat ut omnis impedit voluptas alias."
140
+
141
+ # Default - simple_format
142
+
143
+ order.description_format
144
+ => "<p>Excepturi eum eius sed quia sint fuga veritatis. Nihil nisi omnis ullam voluptatem doloremque iure. Rerum eum recusandae est sit qui odio eum. Et laboriosam voluptas. Beatae qui voluptates velit est soluta. Fugiat ut omnis impedit voluptas alias.</p>"
145
+
146
+
147
+ # truncate
148
+
149
+ order.description_format truncate: true
150
+ => "Excepturi eum eius sed quia..."
151
+
152
+
153
+ order.description_format truncate: {length: 10}
154
+ => "Exceptu..."
155
+
156
+ # word_wrap
157
+
158
+ order.description_format word_wrap: true
159
+ => "Excepturi eum eius sed quia sint fuga veritatis. Nihil nisi omnis ullam\nvoluptatem doloremque iure. Rerum eum recusandae est sit qui odio eum. Et\nlaboriosam voluptas. Beatae qui voluptates velit est soluta. Fugiat ut omnis\nimpedit voluptas alias."
160
+
161
+
162
+ # highlight
163
+
164
+ order.description_format highlight: {phrases: "eum"}
165
+ => "Excepturi <mark>eum</mark> eius sed quia sint fuga veritatis. Nihil nisi omnis ullam voluptatem doloremque iure. Rerum <mark>eum</mark> recusandae est sit qui odio <mark>eum</mark>. Et laboriosam voluptas. Beatae qui voluptates velit est soluta. Fugiat ut omnis impedit voluptas alias."
166
+
167
+
168
+ # excerpt
169
+
170
+ order.description_format excerpt: {phrase: "fuga", radius: 10}
171
+ => "...quia sint fuga veritatis..."
172
+
173
+
174
+ ```
175
+
176
+ <div style="page-break-after: always;"></div>
177
+
178
+ ### **`Model#column_name?`**
179
+
180
+ If you create a column with integer as data type and with a length of 1, this method gets overwritten so it return true only if is equals to 1.
181
+
182
+
183
+ Example using the active attribute of the User class.
184
+
185
+
186
+ ```
187
+ # migration
188
+
189
+ class CreateUsers < ActiveRecord::Migration
190
+ def change
191
+ create_table :users do |t|
192
+ t.string :email
193
+ t.string :first_name
194
+ t.string :last_name
195
+ t.integer :active, limit: 1
196
+ t.datetime :confirmed_at
197
+ t.timestamps null: false
198
+ end
199
+ end
200
+ end
201
+
202
+ ```
203
+
204
+ ```
205
+ user = User.first
206
+
207
+ user.active
208
+ => nil
209
+
210
+ user.active?
211
+ => false
212
+
213
+ user.active = 1
214
+ => 1
215
+
216
+ user.active?
217
+ => true
218
+
219
+ user.active = 0
220
+ => 0
221
+
222
+ user.active?
223
+ => false
224
+
225
+ ```
226
+
227
+
228
+ ### **`Model.truncate(confirm)`**
229
+
230
+ Truncate the table reseting the primary key
231
+
232
+
233
+ ```
234
+ Order.truncate! true
235
+ => (30.0ms) DELETE FROM orders
236
+ (8.5ms) DELETE FROM sqlite_sequence WHERE name='orders'
237
+
238
+ ```
239
+
240
+ **Note:** since `sqlite` does't support the `truncate` statement it will delete the record and the sequence
241
+
242
+
243
+ ---------
data/Rakefile ADDED
@@ -0,0 +1,43 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'SupportUtils'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+ begin
37
+ require 'rspec/core/rake_task'
38
+ RSpec::Core::RakeTask.new(:spec)
39
+ task :default => :spec
40
+ rescue LoadError
41
+ end
42
+
43
+ task default: :test
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module SupportUtils
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module SupportUtils
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>SupportUtils</title>
5
+ <%= stylesheet_link_tag "support_utils/application", media: "all" %>
6
+ <%= javascript_include_tag "support_utils/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ SupportUtils::Engine.routes.draw do
2
+ end
@@ -0,0 +1,5 @@
1
+ require "rails"
2
+ require "support_utils/concerns/core"
3
+ require "support_utils/concerns/utils"
4
+
5
+ require "support_utils/core/active_record/utils"
@@ -0,0 +1,19 @@
1
+ module SupportUtils
2
+ module Concerns
3
+ module Core
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+
8
+ end
9
+
10
+ module ClassMethods
11
+ def has_utils *args
12
+ options = args.extract_options!
13
+ include SupportUtils::Concerns::Utils
14
+ end
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,139 @@
1
+ module SupportUtils
2
+ module Concerns
3
+ module Utils
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+
8
+ self.columns_hash.each do |column_name, metadata|
9
+ self.class_eval <<-RUBY
10
+
11
+ case
12
+ when [:date, :datetime, :time, :timestamp].include?(metadata.type.to_s.to_sym)
13
+
14
+ def #{column_name}_format format = :default, *args
15
+ options = args.extract_options!
16
+ settings = HashWithIndifferentAccess.new({
17
+ format: format
18
+ }).deep_merge(options)
19
+ I18n.l(self.#{column_name}, settings) if #{column_name}
20
+ end
21
+
22
+ when metadata.type == :integer
23
+
24
+ if metadata.limit.to_i == 1
25
+ def #{column_name}? *args
26
+ self.#{column_name}.to_i.eql?(1)
27
+ end
28
+ end
29
+
30
+ when [:float, :decimal].include?(metadata.type.to_s.to_sym)
31
+
32
+ def #{column_name}_format *args
33
+ options = args.extract_options!
34
+ settings = HashWithIndifferentAccess.new({
35
+ precision: 2
36
+ }).deep_merge(options)
37
+ if settings[:currency].present?
38
+ utils.h.number_to_currency self.#{column_name}, *[settings]
39
+ else
40
+ utils.h.number_with_precision self.#{column_name}, *[settings]
41
+ end
42
+ end
43
+
44
+ when [:string, :text].include?(metadata.type.to_s.to_sym)
45
+
46
+ def #{column_name}_format *args, &block
47
+ options = args.extract_options!
48
+ case
49
+ when options[:truncate].present?
50
+
51
+ opts = options[:truncate].is_a?(Hash) ? options[:truncate] : {}
52
+ utils.h.truncate self.#{column_name}.to_s, opts, &block
53
+
54
+ when options[:word_wrap].present?
55
+
56
+ opts = options[:word_wrap].is_a?(Hash) ? options[:word_wrap] : {}
57
+ utils.h.word_wrap self.#{column_name}.to_s, opts, &block
58
+
59
+ when options[:highlight].present?
60
+ opts = options[:highlight].is_a?(Hash) ? options[:highlight] : {}
61
+ phrases = (opts[:phrases].is_a?(Regexp) || opts[:phrases].is_a?(String) || opts[:phrases].is_a?(Array)) ? opts[:phrases] : ""
62
+ utils.h.highlight self.#{column_name}.to_s,phrases, *opts.except(:phrases), &block
63
+
64
+ when options[:excerpt].present?
65
+ opts = options[:excerpt].is_a?(Hash) ? options[:excerpt] : {}
66
+ phrase = (opts[:phrase].is_a?(Regexp) || opts[:phrase].is_a?(String) ) ? opts[:phrase] : ""
67
+ utils.h.excerpt(self.#{column_name}.to_s,phrase, opts.except(:phrase), &block).to_s
68
+
69
+ else
70
+ html_options = options[:html_options] || {}
71
+ utils.h.simple_format(self.#{column_name}, html_options.except(:html_options), options).to_s
72
+ end
73
+ end
74
+
75
+ else
76
+
77
+ def #{column_name}_format *args
78
+ self.#{column_name}
79
+ end
80
+
81
+ end
82
+
83
+ RUBY
84
+ end
85
+
86
+
87
+ end
88
+
89
+ def update_attributes(attributes)
90
+ @saved = super
91
+ end
92
+
93
+ def save(*)
94
+ @saved = super
95
+ end
96
+
97
+ def saved?
98
+ @saved
99
+ end
100
+
101
+
102
+ def utils
103
+ @utils ||= SupportUtils::Core::ActiveRecord::Utils.new(self)
104
+ end
105
+
106
+
107
+ module ClassMethods
108
+
109
+ def truncate! confirm = false
110
+ adapter = ActiveRecord::Base.configurations[Rails.env]["adapter"]
111
+ adapter_method = :"truncate_#{adapter}!"
112
+ if confirm
113
+ if respond_to?(adapter_method, true)
114
+ send(adapter_method)
115
+ else
116
+ ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table_name.to_s}")
117
+ end
118
+ end
119
+ end
120
+
121
+ private
122
+
123
+ # SQLite doesn't have the "TRUNCATE TABLE" command
124
+ def truncate_sqlite3!
125
+ ActiveRecord::Base.connection.execute("DELETE FROM #{table_name.to_s}")
126
+ ActiveRecord::Base.connection.execute("DELETE FROM sqlite_sequence WHERE name='#{table_name.to_s}'")
127
+ end
128
+
129
+ # The "RESTART IDENTITY" Option will restart the primary key
130
+ def truncate_postgresql!
131
+ ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table_name.to_s} RESTART IDENTITY")
132
+ end
133
+
134
+
135
+ end
136
+
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,30 @@
1
+ module SupportUtils
2
+ module Core
3
+ module ActiveRecord
4
+ class Utils
5
+
6
+ attr_reader :model
7
+
8
+ def initialize model
9
+ raise ArgumentError, ":model must be a subclass of ActiveRecord::Base" unless model.class < ::ActiveRecord::Base
10
+ @model = model
11
+ end
12
+
13
+ def time_zone
14
+ Time.zone
15
+ end
16
+
17
+ def helpers
18
+ ActionController::Base.helpers
19
+ end
20
+
21
+ alias_method :h, :helpers
22
+
23
+ def parse_time time
24
+ time_zone.parse(time) rescue nil
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,17 @@
1
+ module SupportUtils
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace SupportUtils
4
+
5
+ config.after_initialize do
6
+ ActiveRecord::Base.send(:include, SupportUtils::Concerns::Core)
7
+ end
8
+
9
+ config.before_initialize do
10
+
11
+ end
12
+
13
+ config.to_prepare do
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module SupportUtils
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "support_utils/all"
2
+ require "support_utils/engine"
3
+
4
+ module SupportUtils
5
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :support_utils do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */