cowtech-rails 1.0.0.0 → 1.1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,94 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the cowtech-rails gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Cowtech
8
+ module RubyOnRails
9
+ module Helpers
10
+ module ApplicationHelper
11
+ def application_info
12
+ @application_info = YAML.load_file(Rails.root + "config/application_info.yml") unless @application_info
13
+ @application_info
14
+ end
15
+
16
+ def location_name(action = nil, controller = nil)
17
+ controller = self.controller_name unless controller
18
+ action = self.action_name unless action
19
+ "#{controller}##{action}"
20
+ end
21
+
22
+ def additional_tag(what = :js)
23
+ if what == :js then
24
+ javascript_include_tag "specific/#{self.controller_name}.js"
25
+ elsif what == :css then
26
+ stylesheet_link_tag "#{self.controller_name}.css"
27
+ end
28
+ end
29
+
30
+ def get_data(key = nil, default = "")
31
+ rv = default
32
+
33
+ unless @outputdata.nil? then
34
+ rv = @outputdata[key] unless @outputdata[key].nil?
35
+ end
36
+
37
+ rv
38
+ end
39
+
40
+ def get_param(key, default = nil)
41
+ if params[key].blank? then default else params[key] end
42
+ end
43
+
44
+ def _normalize_type(format = nil)
45
+ if format != nil then
46
+ request.format = format
47
+ else
48
+ request.format = :text if request.format != :json
49
+ end
50
+ end
51
+
52
+ def setup_json_response(type = :base)
53
+ ApplicationController.setup_json_response(type)
54
+ end
55
+
56
+ def custom_respond_with(data, format = nil)
57
+ return if performed?
58
+
59
+ self._normalize_type(format)
60
+
61
+ if request.format == :text then
62
+ render :text => data
63
+ elsif request.format == :json then
64
+ render :json => data
65
+ end
66
+ end
67
+
68
+ def debug(what, type = :json)
69
+ msg = ""
70
+
71
+ if type == :json then
72
+ begin
73
+ msg = JSON.pretty_generate(what)
74
+ rescue Exception => e
75
+ msg = what.to_json
76
+ end
77
+ else
78
+ msg = what.inspect
79
+ end
80
+
81
+ rv = ""
82
+ case type.to_sym
83
+ when :json
84
+ rv = render_to_string(:json => msg)
85
+ else
86
+ rv = render_to_string(:text => msg)
87
+ end
88
+
89
+ self.response_body = rv
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,302 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the cowtech-rails gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Cowtech
8
+ module RubyOnRails
9
+ module Helpers
10
+ module CRUDHelper
11
+ attr_reader :data_bounds
12
+ attr_reader :record
13
+
14
+ def crud_get_data
15
+ @crud_data ||= {}
16
+ end
17
+
18
+ def crud_get_class(data = nil)
19
+ data = self.crud_get_data unless data
20
+ data[:class].constantize
21
+ end
22
+
23
+ def crud_get_records(data)
24
+ data = self.crud_get_data unless data
25
+ data[:records]
26
+ end
27
+
28
+ def crud_query_get_params(data = nil)
29
+ data = self.crud_get_data unless data
30
+ data[:query_params]
31
+ end
32
+
33
+ def crud_get_sort_order(data = nil)
34
+ data = self.crud_get_data unless data
35
+ data[:sort_order]
36
+ end
37
+
38
+ def crud_has_data?(data = nil)
39
+ data = self.crud_get_data unless data
40
+ data[:data_bounds].total > 0
41
+ end
42
+
43
+ def crud_get_pager_data(data = nil)
44
+ data = self.crud_get_data unless data
45
+ data[:pager_data]
46
+ end
47
+
48
+ def crud_get_form_data(data = nil)
49
+ # TODO: EH?
50
+ @record
51
+ end
52
+
53
+ def crud_get_data_bounds(data = nil)
54
+ data = self.crud_get_data unless data
55
+ data[:data_bounds]
56
+ end
57
+
58
+ def crud_set_data(data)
59
+ @crud_data = data
60
+ end
61
+
62
+ def crud_set_class(data, table = "")
63
+ data = self.crud_get_data unless data
64
+ data[:class] = table
65
+ end
66
+
67
+ def crud_set_records(data, records = nil)
68
+ data = self.crud_get_data unless data
69
+ data[:records] = records
70
+ end
71
+
72
+ def crud_finalize_read(data = nil, records = nil, parameter = :count, per_page = nil)
73
+ data = self.crud_get_data unless data
74
+ records = self.crud_get_records(data) unless records
75
+ self.crud_calculate_data_bounds(data, records, per_page || params[parameter])
76
+ data[:pager_data] = records.paginate(:page => data[:data_bounds].page, :per_page => data[:data_bounds].per_page, :total_entries => data[:data_bounds].total) if records.respond_to?(:paginate)
77
+ end
78
+
79
+ def crud_query_initialize(data = nil, force = false)
80
+ data = self.crud_get_data unless data
81
+ data[:query_expr] = [] if !data[:query_expr] || force
82
+ data[:query_params] = {} if !data[:query_params] || force
83
+ data[:query_initialized] = true
84
+ end
85
+
86
+ def crud_query_get(data = nil, query = nil)
87
+ data = self.crud_get_data unless data
88
+ self.crud_query_initialize(data) unless data[:query_initialized]
89
+ query = data[:query_expr] unless query
90
+
91
+ query.count.times do |i| query[i] = "(#{query[i]})" end
92
+ query.join(" AND ")
93
+ end
94
+
95
+ def crud_query_dump(data = nil)
96
+ data = self.crud_get_data unless data
97
+ self.crud_query_initialize(data) unless data[:query_initialized]
98
+ raise Exception.new("QUERY: #{data[:query_expr]}\nPARAMS: #{data[:query_params].to_json}")
99
+ end
100
+
101
+ def crud_query_add_condition(data, expr, params = {})
102
+ data = self.crud_get_data unless data
103
+ self.crud_query_initialize(data) unless data[:query_initialized]
104
+
105
+ expr = [expr] unless expr.respond_to?(:each)
106
+ expr.each do |e| data[:query_expr] << e end
107
+
108
+ data[:query_params].merge!(params)
109
+ end
110
+
111
+ def crud_query_parse_search(search)
112
+ search = "(@#{search}@)"
113
+ search.gsub!(/(\s+(AND|OR|NOT)\s+)/, "@) \\1 (@")
114
+
115
+ # SOSTITUIAMO I PARAMETRI
116
+ i = -1
117
+ parameters = {}
118
+ search.gsub!(/@(.+?)@/) do |s|
119
+ i += 1
120
+
121
+ key = "search_parameter_#{i}".to_sym
122
+ val = $1
123
+
124
+ # GESTIAMO I MARCATORI DI INIZIO E FINE RIGA
125
+ if val =~ /^\^.+\$$/ then
126
+ val = "#{val.gsub(/^\^(.+)\$$/, "\\1").strip}"
127
+ elsif val =~ /^\^/ then
128
+ val = "#{val.gsub(/^\^/, "").strip}%"
129
+ elsif val =~ /\$$/ then
130
+ val = "%#{val.gsub(/\$$/, "").strip}"
131
+ else
132
+ val = "%#{val.strip}%"
133
+ end
134
+
135
+ parameters[key] = val
136
+ "@FIELD@ LIKE :#{key}"
137
+ end
138
+
139
+ [search, parameters]
140
+ end
141
+
142
+ def crud_handle_search(data, *fields)
143
+ data = self.crud_get_data unless data
144
+ self.crud_handle_extended_search(data, fields)
145
+ end
146
+
147
+ def crud_handle_extended_search(data, fields, externals = nil, args = nil, parameter = :search)
148
+ data = self.crud_get_data unless data
149
+ self.crud_query_initialize(data) unless data[:query_initialized]
150
+ parameter = :search unless parameter
151
+
152
+ self.crud_query_add_condition(data, "(#{self.crud_get_class(data).table_name}.eliminato = :eliminato)", {:eliminato => false}) unless data[:skip_eliminato]
153
+
154
+ # OTTENIAMO LA QUERY
155
+ args = params[parameter] unless args
156
+
157
+ unless args.blank? then
158
+ search, parameters = self.crud_query_parse_search(args)
159
+
160
+ # COMPONIAMO LA QUERY
161
+ data[:query_params].merge!(parameters)
162
+ search_query = []
163
+ fields.each do |field| search_query << "(#{search.gsub("@FIELD@", field.to_s)})" end
164
+
165
+ # ADESSO AGGIUNGIAMO I CAMPI ADDIZIONALI
166
+ if externals then
167
+ externals.each do |external|
168
+ external_query = ""
169
+
170
+ unless external[:manual] then
171
+ external_conds = []
172
+ external.fetch(:fields, []).each do |external_field| external_conds << "(#{search.gsub("@FIELD@", external_field.to_s)})" end
173
+ external_field = external.fetch(:external_field, "id")
174
+ external_query = "(#{external.fetch(:field, "id")} IN (SELECT #{external.fetch(:external_field, "id")} FROM #{external[:table]} WHERE #{external_conds.join(" OR ")}))"
175
+ else
176
+ external_conds = []
177
+ external.fetch(:fields, []).each do |external_field| external_conds << "(#{search.gsub("@FIELD@", external_field.to_s)})" end
178
+ external_query = external[:query].gsub("@SEARCH@", external_conds.join(" OR "))
179
+ end
180
+
181
+ search_query << external_query
182
+ end
183
+ end
184
+
185
+ self.crud_query_add_condition(data, search_query.join(" OR "))
186
+ end
187
+
188
+ [data[:query_expr], data[:query_params]]
189
+ end
190
+
191
+ def crud_handle_sorting(data, default_sorting, sort_data, sort_expression = "@PLACEHOLDER@, updated_at DESC")
192
+ data = self.crud_get_data unless data
193
+ data[:sort_data] = sort_data
194
+ sort = self.crud_get_sort_param(default_sorting, (sort_data || {}).keys)
195
+ data[:sort] = "#{sort.what}-#{sort.how.downcase}"
196
+ data[:sort_order] = sort_expression.gsub("@PLACEHOLDER@", "#{sort.what} #{sort.how}")
197
+ end
198
+
199
+ def crud_form_header(female = false)
200
+ if self.crud_get_form_data.new_record? then
201
+ "Crea nuov#{if female then "a" else "o" end}"
202
+ else
203
+ "Modifica"
204
+ end
205
+ end
206
+
207
+ def crud_form_submit_label
208
+ if self.crud_get_form_data.new_record? then "Inserisci" else "Modifica" end
209
+ end
210
+
211
+ def crud_get_page_param(key = :page, upperbound = -1)
212
+ page = params[key]
213
+ page = if params[key].is_valid_integer? then params[key].to_integer else 1 end
214
+ page = 1 if page < 1
215
+ page = upperbound if (upperbound > 0 and page > upperbound)
216
+ page
217
+ end
218
+
219
+ def crud_get_sort_param(default, valids = [])
220
+ sort_by = get_param(:sort_by, default)
221
+ mo = /^(?<what>[a-z0-9_]+)-(?<how>asc|desc)$/i.match(sort_by)
222
+ mo = /^(?<what>[a-z0-9_]+)-(?<how>asc|desc)$/i.match(default) if !mo || !(valids || []).include?(mo["what"])
223
+
224
+ sf = sort_by.split("-")
225
+ rv = OpenStruct.new({:what => mo["what"], :how => mo["how"].upcase})
226
+
227
+ # ADATTIAMO ALCUNI PARAMETRI
228
+ rv.what = "stato_id" if rv.what == "stato"
229
+
230
+ rv
231
+ end
232
+
233
+ def crud_calculate_data_bounds(data, records = nil, per_page = nil)
234
+ data = self.crud_get_data unless data
235
+ records = data[:records] unless records
236
+ bounds = OpenStruct.new({:total => 0, :first => 0, :last => 0, :pages => 0, :page => 1, :per_page => 1})
237
+
238
+ if records != nil && records.count > 0 then
239
+ per_page = (if per_page.is_valid_integer? then per_page else records[0].class.per_page end).to_integer
240
+ per_page = records.count if per_page < 1
241
+ bounds.total = records.count
242
+ bounds.per_page = per_page
243
+ bounds.pages = (bounds.total.to_f / bounds.per_page).ceil
244
+ bounds.page = self.crud_get_page_param(:page, bounds.pages)
245
+
246
+ base = ((bounds.page - 1) * bounds.per_page)
247
+ bounds.first = base + 1
248
+ bounds.last = base + bounds.per_page
249
+ bounds.last = bounds.total if bounds.last > bounds.total
250
+ end
251
+
252
+ data[:data_bounds] = bounds
253
+ end
254
+
255
+ def crud_update_params
256
+ # TODO: Come fai a backuppare alcuni elementi?
257
+ @agenda_update = params[:agenda_update]
258
+ blacklist = ["controller", "action", "id", "agenda_update"]
259
+ blacklist << "tipo" if self.class.name != "OrdiniController"
260
+ session["params-#{self.location_name}"] = (params.delete_if {|k,v| blacklist.include?(k) || params[k].is_a?(Tempfile)})
261
+ end
262
+
263
+ def crud_yesno
264
+ [OpenStruct.new(:value => true, :label => "Sì"), OpenStruct.new(:value => false, :label => "No")]
265
+ end
266
+
267
+ def crud_end_write_action(additional = nil, absolute = false)
268
+ redirect_to self.crud_end_write_action_url(additional, absolute)
269
+ end
270
+
271
+ def crud_end_write_action_url(additional = nil, absolute = false)
272
+ rp = {}
273
+
274
+ unless absolute then
275
+ rp = session["params-#{self.location_name(:index)}"] || {}
276
+ rp[:action] = :index
277
+ end
278
+
279
+ if additional != nil then
280
+ additional.each do |k, v| rp[k] = v end
281
+ end
282
+
283
+ url_for(rp)
284
+ end
285
+
286
+ def crud_delete(table, id, only_check = false)
287
+ record = table.constantize.safe_find(id.to_integer)
288
+
289
+ if record then
290
+ if only_check then
291
+ record.deletable?(controller.authenticated_user)
292
+ else
293
+ record.delete
294
+ end
295
+ else
296
+ false
297
+ end
298
+ end
299
+ end
300
+ end
301
+ end
302
+ end
@@ -0,0 +1,70 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the cowtech-rails gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Cowtech
8
+ module RubyOnRails
9
+ module Helpers
10
+ module FormatHelper
11
+ def browser
12
+ unless @browser then
13
+ rv = OpenStruct.new({:engine => :other, :version => 1.0})
14
+
15
+ unless request.env['HTTP_USER_AGENT'].blank? then
16
+ ua = request.env['HTTP_USER_AGENT'].downcase
17
+
18
+ if ua.index('msie') and !ua.index('opera') and !ua.index('webtv') then
19
+ rv.engine = :msie
20
+ rv.version = /.+msie ([0-9\.]+).+/.match(ua)[1].to_f
21
+ elsif ua.index('gecko/') or ua.index("mozilla/")
22
+ rv.engine = :gecko
23
+ elsif ua.index('opera')
24
+ rv.engine = :opera
25
+ elsif ua.index('konqueror')
26
+ rv.engine = :konqueror
27
+ elsif ua.index('webkit/')
28
+ rv.engine = :webkit
29
+ end
30
+ end
31
+
32
+ @browser = rv
33
+ end
34
+
35
+ @browser
36
+ end
37
+
38
+ def format_field(field, default = nil)
39
+ if field.is_a?(Fixnum) then
40
+ field
41
+ elsif field.is_a?(Float) then
42
+ field.format_number
43
+ elsif field.blank? || field.strip.blank? then
44
+ (if default then default else "Not set" end)
45
+ else
46
+ field
47
+ end
48
+ end
49
+
50
+ def currency_class(currency, include_positive = true, include_zero = true)
51
+ color = ""
52
+
53
+ if currency > 0 then
54
+ color = "positive" if include_positive
55
+ elsif currency < 0 then
56
+ color = "negative"
57
+ else
58
+ color = "zero" if include_zero
59
+ end
60
+
61
+ "class=\"numeric #{color}\""
62
+ end
63
+
64
+ def text_class(val, additional = nil)
65
+ "class=\"text #{if additional.blank? then nil else additional end} #{if val.blank? then "unset" else nil end}\""
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the cowtech-rails gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Cowtech
8
+ module RubyOnRails
9
+ module Helpers
10
+ module ValidationHelper
11
+ def exists?(cls, query, params)
12
+ cls.constantize.where(query, params).count > 0
13
+ end
14
+
15
+ def json_is_available?(cls, query, params, must_exists = false, internal = false)
16
+ rv = self.setup_json_response(:validator)
17
+
18
+ rv["success"] = true
19
+ rv["valid"] = (self.exists?(cls, query, params) == must_exists)
20
+
21
+ if internal then
22
+ rv
23
+ else
24
+ custom_respond_with(rv.to_json)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -5,7 +5,7 @@
5
5
  #
6
6
 
7
7
  module Cowtech
8
- module RoR
8
+ module RubyOnRails
9
9
  module Models
10
10
  class EMail < ActionMailer::Base
11
11
  def self.setup(method = :smtp)
@@ -5,7 +5,7 @@
5
5
  #
6
6
 
7
7
  module Cowtech
8
- module RoR
8
+ module RubyOnRails
9
9
  module Models
10
10
  class ModelBase < ::ActiveRecord::Base
11
11
  def editable?(user = nil)
@@ -5,7 +5,7 @@
5
5
  #
6
6
 
7
7
  module Cowtech
8
- module RoR
8
+ module RubyOnRails
9
9
  module Extensions
10
10
  module Object
11
11
  include ActionView::Helpers::NumberHelper
@@ -8,7 +8,7 @@ module Cowtech
8
8
  module Rails
9
9
  module Version
10
10
  MAJOR = 1
11
- MINOR = 0
11
+ MINOR = 1
12
12
  PATCH = 0
13
13
  BUILD = 0
14
14
 
data/lib/cowtech.rb CHANGED
@@ -4,12 +4,50 @@
4
4
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
5
  #
6
6
 
7
- if defined?(Rails) && Rails::VERSION::MAJOR == 3 then
8
- require 'engine'
9
- require 'extensions'
10
- require 'monkey_patches'
11
-
12
- dir = File.dirname(__FILE__) + '/../'
13
- require dir + '/app/models/e_mail'
14
- require dir + '/app/models/model_base'
7
+ dir = File.dirname(__FILE__)
8
+
9
+ require 'cowtech/extensions'
10
+ require 'cowtech/monkey_patches'
11
+ #require dir + '/../app/models/e_mail'
12
+ #require dir + '/../app/models/model_base'
13
+
14
+ module Cowtech
15
+ class Engine < Rails::Engine
16
+ end
17
+ end
18
+
19
+ Object.class_eval do
20
+ include Cowtech::RubyOnRails::Extensions::Object
21
+ end
22
+
23
+ TrueClass.class_eval do
24
+ include Cowtech::RubyOnRails::Extensions::True
25
+ end
26
+
27
+ FalseClass.class_eval do
28
+ include Cowtech::RubyOnRails::Extensions::False
29
+ end
30
+
31
+ String.class_eval do
32
+ include Cowtech::RubyOnRails::Extensions::String
15
33
  end
34
+
35
+ Time.class_eval do
36
+ include Cowtech::RubyOnRails::Extensions::DateTime
37
+ end
38
+
39
+ Date.class_eval do
40
+ include Cowtech::RubyOnRails::Extensions::DateTime
41
+ end
42
+
43
+ DateTime.class_eval do
44
+ include Cowtech::RubyOnRails::Extensions::DateTime
45
+ end
46
+
47
+ Hash.class_eval do
48
+ include Cowtech::RubyOnRails::Extensions::Hash
49
+ end
50
+
51
+ Pathname.class_eval do
52
+ include Cowtech::RubyOnRails::Extensions::Pathname
53
+ end
data/rails/init.rb CHANGED
@@ -4,3 +4,4 @@
4
4
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
5
  #
6
6
 
7
+ require 'cowtech'
metadata CHANGED
@@ -4,10 +4,10 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
+ - 1
7
8
  - 0
8
9
  - 0
9
- - 0
10
- version: 1.0.0.0
10
+ version: 1.1.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Shogun
@@ -28,18 +28,16 @@ extensions: []
28
28
  extra_rdoc_files:
29
29
  - README
30
30
  files:
31
- - app/helpers/cowtech/application_helper.rb
32
- - app/helpers/cowtech/crud_helper.rb
33
- - app/helpers/cowtech/format_helper.rb
34
- - app/helpers/cowtech/validation_helper.rb
35
- - app/models/e_mail.rb
36
- - app/models/model_base.rb
37
- - config/initializers/extensions.rb
31
+ - app/helpers/cowtech/ruby_on_rails/helpers/application_helper.rb
32
+ - app/helpers/cowtech/ruby_on_rails/helpers/crud_helper.rb
33
+ - app/helpers/cowtech/ruby_on_rails/helpers/format_helper.rb
34
+ - app/helpers/cowtech/ruby_on_rails/helpers/validation_helper.rb
35
+ - app/models/cowtech/ruby_on_rails/models/e_mail.rb
36
+ - app/models/cowtech/ruby_on_rails/models/model_base.rb
38
37
  - lib/cowtech.rb
39
- - lib/engine.rb
40
- - lib/extensions.rb
41
- - lib/monkey_patches.rb
42
- - lib/version.rb
38
+ - lib/cowtech/extensions.rb
39
+ - lib/cowtech/monkey_patches.rb
40
+ - lib/cowtech/version.rb
43
41
  - rails/init.rb
44
42
  - README
45
43
  has_rdoc: true
@@ -1,90 +0,0 @@
1
- # encoding: utf-8
2
- #
3
- # This file is part of the cowtech-rails gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
- # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
- #
6
-
7
- module Cowtech
8
- module ApplicationHelper
9
- def application_info
10
- @application_info = YAML.load_file(Rails.root + "config/application_info.yml") unless @application_info
11
- @application_info
12
- end
13
-
14
- def location_name(action = nil, controller = nil)
15
- controller = self.controller_name unless controller
16
- action = self.action_name unless action
17
- "#{controller}##{action}"
18
- end
19
-
20
- def additional_tag(what = :js)
21
- if what == :js then
22
- javascript_include_tag "specific/#{self.controller_name}.js"
23
- elsif what == :css then
24
- stylesheet_link_tag "#{self.controller_name}.css"
25
- end
26
- end
27
-
28
- def get_data(key = nil, default = "")
29
- rv = default
30
-
31
- unless @outputdata.nil? then
32
- rv = @outputdata[key] unless @outputdata[key].nil?
33
- end
34
-
35
- rv
36
- end
37
-
38
- def get_param(key, default = nil)
39
- if params[key].blank? then default else params[key] end
40
- end
41
-
42
- def _normalize_type(format = nil)
43
- if format != nil then
44
- request.format = format
45
- else
46
- request.format = :text if request.format != :json
47
- end
48
- end
49
-
50
- def setup_json_response(type = :base)
51
- ApplicationController.setup_json_response(type)
52
- end
53
-
54
- def custom_respond_with(data, format = nil)
55
- return if performed?
56
-
57
- self._normalize_type(format)
58
-
59
- if request.format == :text then
60
- render :text => data
61
- elsif request.format == :json then
62
- render :json => data
63
- end
64
- end
65
-
66
- def debug(what, type = :json)
67
- msg = ""
68
-
69
- if type == :json then
70
- begin
71
- msg = JSON.pretty_generate(what)
72
- rescue Exception => e
73
- msg = what.to_json
74
- end
75
- else
76
- msg = what.inspect
77
- end
78
-
79
- rv = ""
80
- case type.to_sym
81
- when :json
82
- rv = render_to_string(:json => msg)
83
- else
84
- rv = render_to_string(:text => msg)
85
- end
86
-
87
- self.response_body = rv
88
- end
89
- end
90
- end
@@ -1,298 +0,0 @@
1
- # encoding: utf-8
2
- #
3
- # This file is part of the cowtech-rails gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
- # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
- #
6
-
7
- module Cowtech
8
- module CRUDHelper
9
- attr_reader :data_bounds
10
- attr_reader :record
11
-
12
- def crud_get_data
13
- @crud_data ||= {}
14
- end
15
-
16
- def crud_get_class(data = nil)
17
- data = self.crud_get_data unless data
18
- data[:class].constantize
19
- end
20
-
21
- def crud_get_records(data)
22
- data = self.crud_get_data unless data
23
- data[:records]
24
- end
25
-
26
- def crud_query_get_params(data = nil)
27
- data = self.crud_get_data unless data
28
- data[:query_params]
29
- end
30
-
31
- def crud_get_sort_order(data = nil)
32
- data = self.crud_get_data unless data
33
- data[:sort_order]
34
- end
35
-
36
- def crud_has_data?(data = nil)
37
- data = self.crud_get_data unless data
38
- data[:data_bounds].total > 0
39
- end
40
-
41
- def crud_get_pager_data(data = nil)
42
- data = self.crud_get_data unless data
43
- data[:pager_data]
44
- end
45
-
46
- def crud_get_form_data(data = nil)
47
- # TODO: EH?
48
- @record
49
- end
50
-
51
- def crud_get_data_bounds(data = nil)
52
- data = self.crud_get_data unless data
53
- data[:data_bounds]
54
- end
55
-
56
- def crud_set_data(data)
57
- @crud_data = data
58
- end
59
-
60
- def crud_set_class(data, table = "")
61
- data = self.crud_get_data unless data
62
- data[:class] = table
63
- end
64
-
65
- def crud_set_records(data, records = nil)
66
- data = self.crud_get_data unless data
67
- data[:records] = records
68
- end
69
-
70
- def crud_finalize_read(data = nil, records = nil, parameter = :count, per_page = nil)
71
- data = self.crud_get_data unless data
72
- records = self.crud_get_records(data) unless records
73
- self.crud_calculate_data_bounds(data, records, per_page || params[parameter])
74
- data[:pager_data] = records.paginate(:page => data[:data_bounds].page, :per_page => data[:data_bounds].per_page, :total_entries => data[:data_bounds].total) if records.respond_to?(:paginate)
75
- end
76
-
77
- def crud_query_initialize(data = nil, force = false)
78
- data = self.crud_get_data unless data
79
- data[:query_expr] = [] if !data[:query_expr] || force
80
- data[:query_params] = {} if !data[:query_params] || force
81
- data[:query_initialized] = true
82
- end
83
-
84
- def crud_query_get(data = nil, query = nil)
85
- data = self.crud_get_data unless data
86
- self.crud_query_initialize(data) unless data[:query_initialized]
87
- query = data[:query_expr] unless query
88
-
89
- query.count.times do |i| query[i] = "(#{query[i]})" end
90
- query.join(" AND ")
91
- end
92
-
93
- def crud_query_dump(data = nil)
94
- data = self.crud_get_data unless data
95
- self.crud_query_initialize(data) unless data[:query_initialized]
96
- raise Exception.new("QUERY: #{data[:query_expr]}\nPARAMS: #{data[:query_params].to_json}")
97
- end
98
-
99
- def crud_query_add_condition(data, expr, params = {})
100
- data = self.crud_get_data unless data
101
- self.crud_query_initialize(data) unless data[:query_initialized]
102
-
103
- expr = [expr] unless expr.respond_to?(:each)
104
- expr.each do |e| data[:query_expr] << e end
105
-
106
- data[:query_params].merge!(params)
107
- end
108
-
109
- def crud_query_parse_search(search)
110
- search = "(@#{search}@)"
111
- search.gsub!(/(\s+(AND|OR|NOT)\s+)/, "@) \\1 (@")
112
-
113
- # SOSTITUIAMO I PARAMETRI
114
- i = -1
115
- parameters = {}
116
- search.gsub!(/@(.+?)@/) do |s|
117
- i += 1
118
-
119
- key = "search_parameter_#{i}".to_sym
120
- val = $1
121
-
122
- # GESTIAMO I MARCATORI DI INIZIO E FINE RIGA
123
- if val =~ /^\^.+\$$/ then
124
- val = "#{val.gsub(/^\^(.+)\$$/, "\\1").strip}"
125
- elsif val =~ /^\^/ then
126
- val = "#{val.gsub(/^\^/, "").strip}%"
127
- elsif val =~ /\$$/ then
128
- val = "%#{val.gsub(/\$$/, "").strip}"
129
- else
130
- val = "%#{val.strip}%"
131
- end
132
-
133
- parameters[key] = val
134
- "@FIELD@ LIKE :#{key}"
135
- end
136
-
137
- [search, parameters]
138
- end
139
-
140
- def crud_handle_search(data, *fields)
141
- data = self.crud_get_data unless data
142
- self.crud_handle_extended_search(data, fields)
143
- end
144
-
145
- def crud_handle_extended_search(data, fields, externals = nil, args = nil, parameter = :search)
146
- data = self.crud_get_data unless data
147
- self.crud_query_initialize(data) unless data[:query_initialized]
148
- parameter = :search unless parameter
149
-
150
- self.crud_query_add_condition(data, "(#{self.crud_get_class(data).table_name}.eliminato = :eliminato)", {:eliminato => false}) unless data[:skip_eliminato]
151
-
152
- # OTTENIAMO LA QUERY
153
- args = params[parameter] unless args
154
-
155
- unless args.blank? then
156
- search, parameters = self.crud_query_parse_search(args)
157
-
158
- # COMPONIAMO LA QUERY
159
- data[:query_params].merge!(parameters)
160
- search_query = []
161
- fields.each do |field| search_query << "(#{search.gsub("@FIELD@", field.to_s)})" end
162
-
163
- # ADESSO AGGIUNGIAMO I CAMPI ADDIZIONALI
164
- if externals then
165
- externals.each do |external|
166
- external_query = ""
167
-
168
- unless external[:manual] then
169
- external_conds = []
170
- external.fetch(:fields, []).each do |external_field| external_conds << "(#{search.gsub("@FIELD@", external_field.to_s)})" end
171
- external_field = external.fetch(:external_field, "id")
172
- external_query = "(#{external.fetch(:field, "id")} IN (SELECT #{external.fetch(:external_field, "id")} FROM #{external[:table]} WHERE #{external_conds.join(" OR ")}))"
173
- else
174
- external_conds = []
175
- external.fetch(:fields, []).each do |external_field| external_conds << "(#{search.gsub("@FIELD@", external_field.to_s)})" end
176
- external_query = external[:query].gsub("@SEARCH@", external_conds.join(" OR "))
177
- end
178
-
179
- search_query << external_query
180
- end
181
- end
182
-
183
- self.crud_query_add_condition(data, search_query.join(" OR "))
184
- end
185
-
186
- [data[:query_expr], data[:query_params]]
187
- end
188
-
189
- def crud_handle_sorting(data, default_sorting, sort_data, sort_expression = "@PLACEHOLDER@, updated_at DESC")
190
- data = self.crud_get_data unless data
191
- data[:sort_data] = sort_data
192
- sort = self.crud_get_sort_param(default_sorting, (sort_data || {}).keys)
193
- data[:sort] = "#{sort.what}-#{sort.how.downcase}"
194
- data[:sort_order] = sort_expression.gsub("@PLACEHOLDER@", "#{sort.what} #{sort.how}")
195
- end
196
-
197
- def crud_form_header(female = false)
198
- if self.crud_get_form_data.new_record? then
199
- "Crea nuov#{if female then "a" else "o" end}"
200
- else
201
- "Modifica"
202
- end
203
- end
204
-
205
- def crud_form_submit_label
206
- if self.crud_get_form_data.new_record? then "Inserisci" else "Modifica" end
207
- end
208
-
209
- def crud_get_page_param(key = :page, upperbound = -1)
210
- page = params[key]
211
- page = if params[key].is_valid_integer? then params[key].to_integer else 1 end
212
- page = 1 if page < 1
213
- page = upperbound if (upperbound > 0 and page > upperbound)
214
- page
215
- end
216
-
217
- def crud_get_sort_param(default, valids = [])
218
- sort_by = get_param(:sort_by, default)
219
- mo = /^(?<what>[a-z0-9_]+)-(?<how>asc|desc)$/i.match(sort_by)
220
- mo = /^(?<what>[a-z0-9_]+)-(?<how>asc|desc)$/i.match(default) if !mo || !(valids || []).include?(mo["what"])
221
-
222
- sf = sort_by.split("-")
223
- rv = OpenStruct.new({:what => mo["what"], :how => mo["how"].upcase})
224
-
225
- # ADATTIAMO ALCUNI PARAMETRI
226
- rv.what = "stato_id" if rv.what == "stato"
227
-
228
- rv
229
- end
230
-
231
- def crud_calculate_data_bounds(data, records = nil, per_page = nil)
232
- data = self.crud_get_data unless data
233
- records = data[:records] unless records
234
- bounds = OpenStruct.new({:total => 0, :first => 0, :last => 0, :pages => 0, :page => 1, :per_page => 1})
235
-
236
- if records != nil && records.count > 0 then
237
- per_page = (if per_page.is_valid_integer? then per_page else records[0].class.per_page end).to_integer
238
- per_page = records.count if per_page < 1
239
- bounds.total = records.count
240
- bounds.per_page = per_page
241
- bounds.pages = (bounds.total.to_f / bounds.per_page).ceil
242
- bounds.page = self.crud_get_page_param(:page, bounds.pages)
243
-
244
- base = ((bounds.page - 1) * bounds.per_page)
245
- bounds.first = base + 1
246
- bounds.last = base + bounds.per_page
247
- bounds.last = bounds.total if bounds.last > bounds.total
248
- end
249
-
250
- data[:data_bounds] = bounds
251
- end
252
-
253
- def crud_update_params
254
- # TODO: Come fai a backuppare alcuni elementi?
255
- @agenda_update = params[:agenda_update]
256
- blacklist = ["controller", "action", "id", "agenda_update"]
257
- blacklist << "tipo" if self.class.name != "OrdiniController"
258
- session["params-#{self.location_name}"] = (params.delete_if {|k,v| blacklist.include?(k) || params[k].is_a?(Tempfile)})
259
- end
260
-
261
- def crud_yesno
262
- [OpenStruct.new(:value => true, :label => "Sì"), OpenStruct.new(:value => false, :label => "No")]
263
- end
264
-
265
- def crud_end_write_action(additional = nil, absolute = false)
266
- redirect_to self.crud_end_write_action_url(additional, absolute)
267
- end
268
-
269
- def crud_end_write_action_url(additional = nil, absolute = false)
270
- rp = {}
271
-
272
- unless absolute then
273
- rp = session["params-#{self.location_name(:index)}"] || {}
274
- rp[:action] = :index
275
- end
276
-
277
- if additional != nil then
278
- additional.each do |k, v| rp[k] = v end
279
- end
280
-
281
- url_for(rp)
282
- end
283
-
284
- def crud_delete(table, id, only_check = false)
285
- record = table.constantize.safe_find(id.to_integer)
286
-
287
- if record then
288
- if only_check then
289
- record.deletable?(controller.authenticated_user)
290
- else
291
- record.delete
292
- end
293
- else
294
- false
295
- end
296
- end
297
- end
298
- end
@@ -1,66 +0,0 @@
1
- # encoding: utf-8
2
- #
3
- # This file is part of the cowtech-rails gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
- # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
- #
6
-
7
- module Cowtech
8
- module FormatHelper
9
- def browser
10
- unless @browser then
11
- rv = OpenStruct.new({:engine => :other, :version => 1.0})
12
-
13
- unless request.env['HTTP_USER_AGENT'].blank? then
14
- ua = request.env['HTTP_USER_AGENT'].downcase
15
-
16
- if ua.index('msie') and !ua.index('opera') and !ua.index('webtv') then
17
- rv.engine = :msie
18
- rv.version = /.+msie ([0-9\.]+).+/.match(ua)[1].to_f
19
- elsif ua.index('gecko/') or ua.index("mozilla/")
20
- rv.engine = :gecko
21
- elsif ua.index('opera')
22
- rv.engine = :opera
23
- elsif ua.index('konqueror')
24
- rv.engine = :konqueror
25
- elsif ua.index('webkit/')
26
- rv.engine = :webkit
27
- end
28
- end
29
-
30
- @browser = rv
31
- end
32
-
33
- @browser
34
- end
35
-
36
- def format_field(field, default = nil)
37
- if field.is_a?(Fixnum) then
38
- field
39
- elsif field.is_a?(Float) then
40
- field.format_number
41
- elsif field.blank? || field.strip.blank? then
42
- (if default then default else "Not set" end)
43
- else
44
- field
45
- end
46
- end
47
-
48
- def currency_class(currency, include_positive = true, include_zero = true)
49
- color = ""
50
-
51
- if currency > 0 then
52
- color = "positive" if include_positive
53
- elsif currency < 0 then
54
- color = "negative"
55
- else
56
- color = "zero" if include_zero
57
- end
58
-
59
- "class=\"numeric #{color}\""
60
- end
61
-
62
- def text_class(val, additional = nil)
63
- "class=\"text #{if additional.blank? then nil else additional end} #{if val.blank? then "unset" else nil end}\""
64
- end
65
- end
66
- end
@@ -1,26 +0,0 @@
1
- # encoding: utf-8
2
- #
3
- # This file is part of the cowtech-rails gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
- # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
- #
6
-
7
- module Cowtech
8
- module ValidationHelper
9
- def exists?(cls, query, params)
10
- cls.constantize.where(query, params).count > 0
11
- end
12
-
13
- def json_is_available?(cls, query, params, must_exists = false, internal = false)
14
- rv = self.setup_json_response(:validator)
15
-
16
- rv["success"] = true
17
- rv["valid"] = (self.exists?(cls, query, params) == must_exists)
18
-
19
- if internal then
20
- rv
21
- else
22
- custom_respond_with(rv.to_json)
23
- end
24
- end
25
- end
26
- end
@@ -1,41 +0,0 @@
1
- # encoding: utf-8
2
- #
3
- # This file is part of the cowtech-rails gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
- # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
- #
6
-
7
- Object.class_eval do
8
- include Cowtech::RoR::Extensions::Object
9
- end
10
-
11
- TrueClass.class_eval do
12
- include Cowtech::RoR::Extensions::True
13
- end
14
-
15
- FalseClass.class_eval do
16
- include Cowtech::RoR::Extensions::False
17
- end
18
-
19
- String.class_eval do
20
- include Cowtech::RoR::Extensions::String
21
- end
22
-
23
- Time.class_eval do
24
- include Cowtech::RoR::Extensions::DateTime
25
- end
26
-
27
- Date.class_eval do
28
- include Cowtech::RoR::Extensions::DateTime
29
- end
30
-
31
- DateTime.class_eval do
32
- include Cowtech::RoR::Extensions::DateTime
33
- end
34
-
35
- Hash.class_eval do
36
- include Cowtech::RoR::Extensions::Hash
37
- end
38
-
39
- Pathname.class_eval do
40
- include Cowtech::RoR::Extensions::Pathname
41
- end
data/lib/engine.rb DELETED
@@ -1,10 +0,0 @@
1
- # encoding: utf-8
2
- #
3
- # This file is part of the cowtech-rails gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
- # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
- #
6
-
7
- module Cowtech
8
- class Engine < Rails::Engine
9
- end
10
- end