cowtech-rails 2.8.4 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +19 -0
- data/app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb +3 -3
- data/app/helpers/cowtech/ruby_on_rails/helpers/browser_helper.rb +1 -1
- data/app/helpers/cowtech/ruby_on_rails/helpers/debug_helper.rb +12 -12
- data/app/helpers/cowtech/ruby_on_rails/helpers/mongoid_crud_helper.rb +7 -7
- data/app/models/cowtech/ruby_on_rails/models/ar/model_base.rb +88 -88
- data/app/models/cowtech/ruby_on_rails/models/e_mail.rb +2 -2
- data/app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb +104 -104
- data/app/models/cowtech/ruby_on_rails/models/mongoid/logging.rb +25 -25
- data/app/models/cowtech/ruby_on_rails/models/mongoid/sequence.rb +50 -50
- data/lib/cowtech/extensions.rb +1 -1
- data/lib/cowtech/scheduler.rb +9 -9
- data/lib/cowtech/tasks/app.rake +2 -2
- data/lib/cowtech/tasks/server.rake +1 -1
- data/lib/cowtech/tasks/sql.rake +3 -3
- data/lib/cowtech/version.rb +3 -3
- data/rails/init.rb +1 -1
- metadata +18 -7
- data/README +0 -6
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= cowtech-rails
|
2
|
+
|
3
|
+
General purpose Ruby on Rails library.
|
4
|
+
http://github.com/ShogunPanda/cowtech-rails
|
5
|
+
|
6
|
+
== Contributing to cowtech-rails
|
7
|
+
|
8
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
9
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
10
|
+
* Fork the project
|
11
|
+
* Start a feature/bugfix branch
|
12
|
+
* Commit and push until you are happy with your contribution
|
13
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
14
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
|
19
|
+
Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
@@ -68,7 +68,7 @@ module Cowtech
|
|
68
68
|
data = self.crud_get_data if !data
|
69
69
|
records = self.crud_get_records(data) if !records
|
70
70
|
self.crud_calculate_data_bounds(data, records, per_page || params[parameter])
|
71
|
-
data[:pager_data] = records.paginate(:
|
71
|
+
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)
|
72
72
|
end
|
73
73
|
|
74
74
|
def crud_query_initialize(data = nil, force = false)
|
@@ -228,7 +228,7 @@ module Cowtech
|
|
228
228
|
mo = /^(?<what>[a-z0-9_]+)-(?<how>asc|desc)$/i.match(default) if !mo || !((valids || []).include?(mo["what"]))
|
229
229
|
|
230
230
|
sf = sort_by.split("-")
|
231
|
-
rv = OpenStruct.new({:
|
231
|
+
rv = OpenStruct.new({what: mo["what"], how: mo["how"].upcase})
|
232
232
|
|
233
233
|
# Adapt some parameters
|
234
234
|
rv.what = "status_id" if rv.what == "status"
|
@@ -239,7 +239,7 @@ module Cowtech
|
|
239
239
|
def crud_calculate_data_bounds(data, records = nil, per_page = nil)
|
240
240
|
data = self.crud_get_data if !data
|
241
241
|
records = data[:records] if !records
|
242
|
-
bounds = OpenStruct.new({:
|
242
|
+
bounds = OpenStruct.new({total: 0, first: 0, last: 0, pages: 0, page: 1, per_page: 1})
|
243
243
|
|
244
244
|
if records != nil && records.count > 0 then
|
245
245
|
per_page = (per_page.is_valid_integer? ? per_page : records[0].class.per_page).to_integer
|
@@ -9,7 +9,7 @@ module Cowtech
|
|
9
9
|
module Helpers
|
10
10
|
module BrowserHelper
|
11
11
|
def browser_detect
|
12
|
-
rv = {:
|
12
|
+
rv = {engine: :unknown, version: "0", platform: :unknown, agent: request.user_agent || request.env["HTTP_USER_AGENT"].try(:downcase) || ""}
|
13
13
|
|
14
14
|
if rv[:agent].present? then
|
15
15
|
agent = rv[:agent].downcase
|
@@ -5,36 +5,36 @@
|
|
5
5
|
#
|
6
6
|
|
7
7
|
module Cowtech
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
module RubyOnRails
|
9
|
+
module Helpers
|
10
|
+
module DebugHelper
|
11
11
|
attr_reader :debug_msg
|
12
|
-
|
12
|
+
|
13
13
|
def debug_timestamp
|
14
14
|
"[" + Time.now.strftime("%F %T.%L %z") + "]"
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def debug_file
|
18
18
|
@debug_file ||= Logger.new(Rails.root + "log/debug.log")
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def debug_msgs
|
22
22
|
@debug_msgs ||= []
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def debug_dump_object(object, target = nil, method = :to_yaml)
|
26
26
|
self.debug_log("OBJECT DUMP", object.send(method).ensure_string, target)
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
def debug_log(tags, msg, target = nil, no_timestamp = false)
|
30
30
|
tags = tags.ensure_array if tags.present?
|
31
|
-
|
32
|
-
final_msg = []
|
31
|
+
|
32
|
+
final_msg = []
|
33
33
|
final_msg << self.debug_timestamp if !no_timestamp
|
34
34
|
tags.collect { |tag| "[" + tag + "]" }.each { |tag| final_msg << tag } if tags.present?
|
35
|
-
final_msg << msg
|
35
|
+
final_msg << msg
|
36
36
|
final_msg = final_msg.join(" ")
|
37
|
-
|
37
|
+
|
38
38
|
if !target.nil? && target.respond_to?(:debug) then
|
39
39
|
self.debug_file.debug(final_msg)
|
40
40
|
else
|
@@ -21,8 +21,8 @@ module Cowtech
|
|
21
21
|
@mongo_class = @mongo_class.constantize if @mongo_class.is_a?(String)
|
22
22
|
@mongo_records = []
|
23
23
|
@mongo_sort_order = [[:_id, :asc]]
|
24
|
-
@mongo_bounds = {:
|
25
|
-
@mongo_query = self.mongo_reset_query(:
|
24
|
+
@mongo_bounds = {total: 0, first: 1, last: 0, pages: 1, page: 1, per_page: 1}
|
25
|
+
@mongo_query = self.mongo_reset_query(also_deleted: args[:also_deleted]) if @mongo_class
|
26
26
|
end
|
27
27
|
|
28
28
|
def mongo_has_data?(args = {})
|
@@ -36,12 +36,12 @@ module Cowtech
|
|
36
36
|
@mongo_bounds[:pages] = (@mongo_bounds[:total].to_f / @mongo_bounds[:per_page]).ceil
|
37
37
|
|
38
38
|
if @mongo_bounds[:per_page] > 0 then
|
39
|
-
@mongo_bounds[:page] = self.mongo_get_page_param(:
|
39
|
+
@mongo_bounds[:page] = self.mongo_get_page_param(upperbound: @mongo_bounds[:pages])
|
40
40
|
base = ((@mongo_bounds[:page] - 1) * @mongo_bounds[:per_page])
|
41
41
|
@mongo_bounds[:first] = base + 1
|
42
42
|
@mongo_bounds[:last] = [base + @mongo_bounds[:per_page], @mongo_bounds[:total]].min
|
43
43
|
else
|
44
|
-
@mongo_bounds.merge!(:
|
44
|
+
@mongo_bounds.merge!(pages: 1, page: 1, first: 1, last: @mongo_bounds[:total], per_page: @mongo_bounds[:total])
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
@@ -50,7 +50,7 @@ module Cowtech
|
|
50
50
|
|
51
51
|
def mongo_fetch_data(args = {})
|
52
52
|
@mongo_records = @mongo_query.order_by(@mongo_sort_order)
|
53
|
-
self.mongo_calculate_bounds(args.reverse_merge(:
|
53
|
+
self.mongo_calculate_bounds(args.reverse_merge(per_page: (args[:per_page] || @mongo_per_page || params[args[:parameter] || :count])))
|
54
54
|
@records = @mongo_records.skip(@mongo_bounds[:first] - 1).limit(@mongo_bounds[:per_page])
|
55
55
|
@mongo_pager = WillPaginate::Collection.new(@mongo_bounds[:page], @mongo_bounds[:per_page], @mongo_bounds[:total])
|
56
56
|
end
|
@@ -69,7 +69,7 @@ module Cowtech
|
|
69
69
|
if condition.is_a?(Hash) then
|
70
70
|
condition.each_pair do |key, val|
|
71
71
|
if key == "$or" then
|
72
|
-
@mongo_query = @mongo_query.any_of(val) # TODO: This
|
72
|
+
@mongo_query = @mongo_query.any_of(val) # TODO: This doesn't work as expected. See: https://github.com/mongoid/mongoid/issues/569
|
73
73
|
else
|
74
74
|
@mongo_query = @mongo_query.where({key => val})
|
75
75
|
end
|
@@ -149,7 +149,7 @@ module Cowtech
|
|
149
149
|
order = args[:order] || [:current, [:updated_at, :desc]]
|
150
150
|
|
151
151
|
# Get current request sort order and then replace it into the sort fields
|
152
|
-
current = self.mongo_get_sort_param(:
|
152
|
+
current = self.mongo_get_sort_param(default: args[:default])
|
153
153
|
current_index = order.index(:current)
|
154
154
|
order[current_index] = current if current_index
|
155
155
|
|
@@ -5,93 +5,93 @@
|
|
5
5
|
#
|
6
6
|
|
7
7
|
module Cowtech
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
def self.status_column
|
18
|
-
"status_id"
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.deleted_status_id
|
22
|
-
0
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.[](what, only_id = false)
|
26
|
-
self.__finalize(self.__safe_index_find(what), only_id)
|
27
|
-
end
|
28
|
-
|
29
|
-
def safe_id
|
30
|
-
self.id || 0
|
31
|
-
end
|
32
|
-
|
33
|
-
def editable?(user = nil)
|
34
|
-
true
|
35
|
-
end
|
36
|
-
|
37
|
-
def deletable?(user = nil)
|
38
|
-
true
|
39
|
-
end
|
40
|
-
|
41
|
-
def delete(definitive = false)
|
42
|
-
if !definitive then
|
43
|
-
if self.deletable? then
|
44
|
-
if self.has_attribute?(self.class.deleted_column) then
|
45
|
-
self.update_attribute(self.class.deleted_column, DateTime.now)
|
46
|
-
true
|
47
|
-
elsif self.has_attribute?(self.class.status_column) then
|
48
|
-
self.update_attribute(self.class.status_column, self.deleted_status)
|
49
|
-
true
|
50
|
-
else
|
51
|
-
super()
|
52
|
-
end
|
53
|
-
else
|
54
|
-
false
|
55
|
-
end
|
56
|
-
else
|
57
|
-
super()
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def is?(other)
|
62
|
-
other ? (self.id == self.class.__safe_index_find(other).id) : false
|
63
|
-
end
|
8
|
+
module RubyOnRails
|
9
|
+
module Models
|
10
|
+
module Ar
|
11
|
+
if defined?(ActiveRecord) then
|
12
|
+
class ModelBase < ::ActiveRecord::Base
|
13
|
+
def self.deleted_column
|
14
|
+
"deleted_at"
|
15
|
+
end
|
64
16
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
17
|
+
def self.status_column
|
18
|
+
"status_id"
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.deleted_status_id
|
22
|
+
0
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.[](what, only_id = false)
|
26
|
+
self.__finalize(self.__safe_index_find(what), only_id)
|
27
|
+
end
|
28
|
+
|
29
|
+
def safe_id
|
30
|
+
self.id || 0
|
31
|
+
end
|
32
|
+
|
33
|
+
def editable?(user = nil)
|
34
|
+
true
|
35
|
+
end
|
36
|
+
|
37
|
+
def deletable?(user = nil)
|
38
|
+
true
|
39
|
+
end
|
40
|
+
|
41
|
+
def delete(definitive = false)
|
42
|
+
if !definitive then
|
43
|
+
if self.deletable? then
|
44
|
+
if self.has_attribute?(self.class.deleted_column) then
|
45
|
+
self.update_attribute(self.class.deleted_column, DateTime.now)
|
46
|
+
true
|
47
|
+
elsif self.has_attribute?(self.class.status_column) then
|
48
|
+
self.update_attribute(self.class.status_column, self.deleted_status)
|
49
|
+
true
|
50
|
+
else
|
51
|
+
super()
|
52
|
+
end
|
53
|
+
else
|
54
|
+
false
|
55
|
+
end
|
56
|
+
else
|
57
|
+
super()
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def is?(other)
|
62
|
+
other ? (self.id == self.class.__safe_index_find(other).id) : false
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
def self.__index_find(what)
|
67
|
+
self.find(what)
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.__finalize(record, only_id = false)
|
71
|
+
if record then
|
72
|
+
only_id ? record.id : record
|
73
|
+
else
|
74
|
+
nil
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.__safe_index_find(what)
|
79
|
+
record = nil
|
80
|
+
|
81
|
+
begin
|
82
|
+
record = self.__index_find(what)
|
83
|
+
rescue ActiveRecord::RecordNotFound
|
84
|
+
record = nil
|
85
|
+
end
|
86
|
+
|
87
|
+
record
|
88
|
+
end
|
89
|
+
end
|
90
|
+
else
|
91
|
+
class ModelBase
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
97
|
end
|
@@ -61,10 +61,10 @@ module Cowtech
|
|
61
61
|
|
62
62
|
mail(args) do |format|
|
63
63
|
if plain_body then
|
64
|
-
format.text { render :
|
64
|
+
format.text { render text: plain_body }
|
65
65
|
end
|
66
66
|
if html_body then
|
67
|
-
format.html { render :
|
67
|
+
format.html { render text: html_body }
|
68
68
|
end
|
69
69
|
end
|
70
70
|
end
|
@@ -5,108 +5,108 @@
|
|
5
5
|
#
|
6
6
|
|
7
7
|
module Cowtech
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
8
|
+
module RubyOnRails
|
9
|
+
module Models
|
10
|
+
module Mongoid
|
11
|
+
module Cowtech
|
12
|
+
extend ActiveSupport::Concern
|
13
|
+
|
14
|
+
# Uncomment for numeric ID
|
15
|
+
# included do
|
16
|
+
# include Mongoid::Sequence
|
17
|
+
# identity type: Integer
|
18
|
+
# sequence :_id
|
19
|
+
# end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
def [](what, only_id = false)
|
23
|
+
self.__finalize(self.__safe_index_find(what), only_id)
|
24
|
+
end
|
25
|
+
|
26
|
+
def find_or_create(oid, attributes = nil)
|
27
|
+
self.safe_find(oid) || self.new(attributes)
|
28
|
+
end
|
29
|
+
|
30
|
+
def safe_find(oid)
|
31
|
+
rv = oid.blank? ? nil : self.find(BSON::ObjectId(oid))
|
32
|
+
rescue ::Mongoid::Errors::DocumentNotFound, BSON::InvalidObjectId
|
33
|
+
nil
|
34
|
+
end
|
35
|
+
|
36
|
+
def random
|
37
|
+
c = self.count
|
38
|
+
c != 0 ? self.skip(rand(c)).first : nil
|
39
|
+
end
|
40
|
+
|
41
|
+
def per_page
|
42
|
+
25
|
43
|
+
end
|
44
|
+
|
45
|
+
# Overrides for paranoia module to allow find associations on deleted documents
|
46
|
+
def criteria(*args)
|
47
|
+
rv = super
|
48
|
+
rv.selector = {}
|
49
|
+
rv
|
50
|
+
end
|
51
|
+
|
52
|
+
def not_deleted
|
53
|
+
where(:deleted_at.exists => false)
|
54
|
+
end
|
55
|
+
|
56
|
+
def valid_object_id?(oid)
|
57
|
+
oid.blank? || BSON::ObjectId.legal?(oid)
|
58
|
+
end
|
59
|
+
|
60
|
+
def __index_find(oid)
|
61
|
+
oid.blank? ? nil : self.find(BSON::ObjectId(oid))
|
62
|
+
rescue ::Mongoid::Errors::DocumentNotFound, BSON::InvalidObjectId
|
63
|
+
nil
|
64
|
+
end
|
65
|
+
|
66
|
+
def __finalize(record, only_id = false)
|
67
|
+
record ? (only_id ? record.id : record) : nil
|
68
|
+
end
|
69
|
+
|
70
|
+
def __safe_index_find(what)
|
71
|
+
self.__index_find(what)
|
72
|
+
rescue ::Mongoid::Errors::DocumentNotFound, BSON::InvalidObjectId
|
73
|
+
nil
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
module InstanceMethods
|
78
|
+
# Decommentare for numeric type ids
|
79
|
+
# def safe_id
|
80
|
+
# self.id ? self.id : 0
|
81
|
+
# end
|
82
|
+
|
83
|
+
def editable?(user = nil)
|
84
|
+
true
|
85
|
+
end
|
86
|
+
|
87
|
+
def deletable?(user = nil)
|
88
|
+
true
|
89
|
+
end
|
90
|
+
|
91
|
+
def delete(definitive = false)
|
92
|
+
if definitive != true then
|
93
|
+
if self.deletable? then
|
94
|
+
super()
|
95
|
+
true
|
96
|
+
else
|
97
|
+
false
|
98
|
+
end
|
99
|
+
else
|
100
|
+
self.delete!
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def is?(other)
|
105
|
+
other ? (self.id == self.class.__safe_index_find(other).id) : false
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
112
|
end
|
@@ -5,30 +5,30 @@
|
|
5
5
|
#
|
6
6
|
|
7
7
|
module Cowtech
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
included do
|
15
|
-
set_callback(:create, :after) { |d| d.log_activity(:create) }
|
16
|
-
set_callback(:update, :after) { |d| d.log_activity(:update) }
|
17
|
-
end
|
18
|
-
|
19
|
-
module InstanceMethods
|
20
|
-
def delete(options = {})
|
21
|
-
log_activity(:delete)
|
22
|
-
super(options)
|
23
|
-
end
|
8
|
+
module RubyOnRails
|
9
|
+
module Models
|
10
|
+
module Mongoid
|
11
|
+
module Logging
|
12
|
+
extend ActiveSupport::Concern
|
24
13
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
14
|
+
included do
|
15
|
+
set_callback(:create, :after) { |d| d.log_activity(:create) }
|
16
|
+
set_callback(:update, :after) { |d| d.log_activity(:update) }
|
17
|
+
end
|
18
|
+
|
19
|
+
module InstanceMethods
|
20
|
+
def delete(options = {})
|
21
|
+
log_activity(:delete)
|
22
|
+
super(options)
|
23
|
+
end
|
24
|
+
|
25
|
+
def restore
|
26
|
+
log_activity(:restore)
|
27
|
+
super
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
34
|
end
|
@@ -5,55 +5,55 @@
|
|
5
5
|
#
|
6
6
|
|
7
7
|
module Cowtech
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
module ClassMethods
|
24
|
-
def sequence(_field)
|
25
|
-
# REPLACE FIELD DEFAULT VALUE
|
26
|
-
_field = _field.to_s
|
27
|
-
field(_field, fields[_field].options.merge(:default => lambda{ self.class.set_from_sequence(_field)}))
|
28
|
-
end
|
29
|
-
|
30
|
-
def set_from_sequence(_field)
|
31
|
-
sequences = self.db.collection("__sequences")
|
32
|
-
counter_id = "#{self.class.name.underscore}_#{_field}"
|
8
|
+
module RubyOnRails
|
9
|
+
module Models
|
10
|
+
module Mongoid
|
11
|
+
# Include this module to add automatic sequence feature (also works for _id field, so SQL-Like autoincrement primary key can easily be simulated)
|
12
|
+
# usage:
|
13
|
+
# class KlassName
|
14
|
+
# include Mongoid::Document
|
15
|
+
# include Mongoid::Sequence
|
16
|
+
# ...
|
17
|
+
# field :number, :type=>Integer
|
18
|
+
# sequence :number
|
19
|
+
# ...
|
20
|
+
module Sequence
|
21
|
+
extend ActiveSupport::Concern
|
33
22
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
23
|
+
module ClassMethods
|
24
|
+
def sequence(_field)
|
25
|
+
# REPLACE FIELD DEFAULT VALUE
|
26
|
+
_field = _field.to_s
|
27
|
+
field(_field, fields[_field].options.merge(default: lambda{ self.class.set_from_sequence(_field)}))
|
28
|
+
end
|
29
|
+
|
30
|
+
def set_from_sequence(_field)
|
31
|
+
sequences = self.db.collection("__sequences")
|
32
|
+
counter_id = "#{self.class.name.underscore}_#{_field}"
|
33
|
+
|
34
|
+
# Increase the sequence value and also avoids conflicts
|
35
|
+
catch(:value) do
|
36
|
+
value = nil
|
37
|
+
begin
|
38
|
+
value = sequences.find_and_modify(
|
39
|
+
query: {"_id" => counter_id},
|
40
|
+
:update=> {"$inc" => {"value" => 1}},
|
41
|
+
new: true,
|
42
|
+
upsert: true
|
43
|
+
).send("[]", "value")
|
44
|
+
end while self.first({conditions: {_field => value}})
|
45
|
+
throw :value, value
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def reset_sequence(_field)
|
50
|
+
sequences = self.db.collection("__sequences")
|
51
|
+
counter_id = "#{self.class.name.underscore}_#{_field.to_s}"
|
52
|
+
sequences.find_and_modify(query: {"_id" => counter_id}, :update=> {"$set" => {"value" => 0}}, new: true, upsert: true)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
59
|
end
|
data/lib/cowtech/extensions.rb
CHANGED
data/lib/cowtech/scheduler.rb
CHANGED
@@ -56,27 +56,27 @@ module Cowtech
|
|
56
56
|
task = Rake::Task[name]
|
57
57
|
values = task.arg_names.collect { |a| args[a.to_sym] }
|
58
58
|
|
59
|
-
self.log(label + args_string + " ...", {:
|
59
|
+
self.log(label + args_string + " ...", {prefix: ["RAKE", "START", name]})
|
60
60
|
task.reenable
|
61
61
|
task.invoke(*values)
|
62
|
-
self.log("Rake task ended.", {:
|
62
|
+
self.log("Rake task ended.", {prefix: ["RAKE", "END", name]})
|
63
63
|
rescue Exception => e
|
64
|
-
self.log("Rake task failed with exception: [#{e.class.to_s}] #{e.to_s}.", {:
|
64
|
+
self.log("Rake task failed with exception: [#{e.class.to_s}] #{e.to_s}.", {prefix: ["RAKE", "ERROR", name]})
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
68
|
def execute_inline_task(label, name)
|
69
69
|
begin
|
70
|
-
self.log(label + " ...", {:
|
70
|
+
self.log(label + " ...", {prefix: ["INLINE", "START", name]})
|
71
71
|
yield if block_given?
|
72
|
-
self.log("Inline task ended.", {:
|
72
|
+
self.log("Inline task ended.", {prefix: ["INLINE", "END", name]})
|
73
73
|
rescue Exception => e
|
74
|
-
self.log("Inline task failed with exception: [#{e.class.to_s}] #{e.to_s}.", {:
|
74
|
+
self.log("Inline task failed with exception: [#{e.class.to_s}] #{e.to_s}.", {prefix: ["RAKE", "ERROR", name]})
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
78
|
def execute
|
79
|
-
self.log("Scheduler started.", {:
|
79
|
+
self.log("Scheduler started.", {prefix: "MAIN"})
|
80
80
|
self.handle_plain
|
81
81
|
end
|
82
82
|
|
@@ -86,7 +86,7 @@ module Cowtech
|
|
86
86
|
|
87
87
|
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
88
88
|
if forked && !FileTest.exists?(@pid) then
|
89
|
-
self.log("Starting process with PID #{$$}", {:
|
89
|
+
self.log("Starting process with PID #{$$}", {prefix: ["WORKER", "START"]})
|
90
90
|
File.open(@pid, "w") { |f| f.write($$) }
|
91
91
|
self.handle_plain
|
92
92
|
end
|
@@ -95,7 +95,7 @@ module Cowtech
|
|
95
95
|
PhusionPassenger.on_event(:stopping_worker_process) do
|
96
96
|
if FileTest.exists?(@pid) then
|
97
97
|
if File.open(@pid, "r") { |f| pid = f.read.to_i} == $$ then
|
98
|
-
self.log("Stopped process with PID #{$$}", {:
|
98
|
+
self.log("Stopped process with PID #{$$}", {prefix: ["WORKER", "STOP"]})
|
99
99
|
File.delete(@pid)
|
100
100
|
end
|
101
101
|
end
|
data/lib/cowtech/tasks/app.rake
CHANGED
@@ -70,7 +70,7 @@ namespace :app do
|
|
70
70
|
end
|
71
71
|
|
72
72
|
desc "Clears all Rails cache"
|
73
|
-
task :
|
73
|
+
task clear_cache: :environment do |task|
|
74
74
|
Cowtech::RubyOnRails::AppUtils.clear_cache
|
75
75
|
end
|
76
76
|
|
@@ -99,7 +99,7 @@ end
|
|
99
99
|
|
100
100
|
namespace :css do
|
101
101
|
desc "Regenerating CSS..."
|
102
|
-
task :
|
102
|
+
task regenerate: :environment do |task|
|
103
103
|
puts "Regenerating CSS..."
|
104
104
|
|
105
105
|
if defined?(Less) then # More
|
@@ -85,7 +85,7 @@ end
|
|
85
85
|
namespace :server do
|
86
86
|
namespace :thin do
|
87
87
|
desc "Starts Thin server"
|
88
|
-
task :start, ["environment", "config"]
|
88
|
+
task :start, ["environment", "config"] do |task, rake_args|
|
89
89
|
Cowtech::RubyOnRails::ThinServer.start(rake_args.with_defaults("config" => Cowtech::RubyOnRails::ServerUtils.thin_path, "environment" => Cowtech::RubyOnRails::ServerUtils.default_environment))
|
90
90
|
end
|
91
91
|
|
data/lib/cowtech/tasks/sql.rake
CHANGED
@@ -68,7 +68,7 @@ module Cowtech
|
|
68
68
|
(ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
|
69
69
|
Cowtech::RubyOnRails::Scheduler.log "--- --- Dumping table #{table_name} ..."
|
70
70
|
i = "01"
|
71
|
-
File.open("#{RAILS_ROOT}/test/fixture/#{table_name}.yml",
|
71
|
+
File.open("#{RAILS_ROOT}/test/fixture/#{table_name}.yml", "w") do |file|
|
72
72
|
data = ActiveRecord::Base.connection.select_all(sql % table_name)
|
73
73
|
file.write data.inject({}) { |hash, record|
|
74
74
|
hash["#{table_name}_#{i.succ!}"] = record
|
@@ -104,8 +104,8 @@ module Cowtech
|
|
104
104
|
end
|
105
105
|
|
106
106
|
namespace :mysql do
|
107
|
-
desc
|
108
|
-
task :
|
107
|
+
desc "Converting data to fixtures"
|
108
|
+
task to_fixtures: :environment do
|
109
109
|
Cowtech::RubyOnRails::SqlUtils.to_fixtures
|
110
110
|
end
|
111
111
|
|
data/lib/cowtech/version.rb
CHANGED
data/rails/init.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cowtech-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-16 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: jeweler
|
16
|
+
requirement: &70184745284020 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70184745284020
|
14
25
|
- !ruby/object:Gem::Dependency
|
15
26
|
name: cowtech-extensions
|
16
|
-
requirement: &
|
27
|
+
requirement: &70184745283180 !ruby/object:Gem::Requirement
|
17
28
|
none: false
|
18
29
|
requirements:
|
19
30
|
- - ! '>='
|
@@ -21,13 +32,13 @@ dependencies:
|
|
21
32
|
version: '0'
|
22
33
|
type: :runtime
|
23
34
|
prerelease: false
|
24
|
-
version_requirements: *
|
35
|
+
version_requirements: *70184745283180
|
25
36
|
description: A general purpose Rails utility plugin.
|
26
37
|
email: shogun_panda@me.com
|
27
38
|
executables: []
|
28
39
|
extensions: []
|
29
40
|
extra_rdoc_files:
|
30
|
-
- README
|
41
|
+
- README.rdoc
|
31
42
|
files:
|
32
43
|
- app/helpers/cowtech/ruby_on_rails/helpers/application_helper.rb
|
33
44
|
- app/helpers/cowtech/ruby_on_rails/helpers/ar_crud_helper.rb
|
@@ -52,7 +63,7 @@ files:
|
|
52
63
|
- lib/cowtech/tasks/sql.rake
|
53
64
|
- lib/cowtech/version.rb
|
54
65
|
- rails/init.rb
|
55
|
-
- README
|
66
|
+
- README.rdoc
|
56
67
|
homepage: http://github.com/ShogunPanda/cowtech-rails
|
57
68
|
licenses:
|
58
69
|
- MIT
|
@@ -65,7 +76,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
65
76
|
requirements:
|
66
77
|
- - ! '>='
|
67
78
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
79
|
+
version: 1.9.2
|
69
80
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
81
|
none: false
|
71
82
|
requirements:
|