dcidev_active_record 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dcidev_active_record.rb +101 -116
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81d43d6c23726bfcc5dd25fb8d4cfe17e30ba7f7a11a2c12f7999926e7e7bcae
|
4
|
+
data.tar.gz: b66a24c0c256baf7eb1912e19d574c133e3b0b009340db9d90d59e95b7615d52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 971004a905d4f940e0ea2649ec22262410577570ca051d59d2b72f3a11f5df06471ae97e6953d355609dabfc7789819d0ca24a011a9a8b72d71d3417bdc46d65
|
7
|
+
data.tar.gz: 450e364d5d0cbc5aa5ed671097cf323ef7cda6d5a518a0debfd05a3309e1500bd24ec807745d1b0c04f7b1713d904ab2e26743293d75d9d9185d7b2cfe0e78d3
|
data/lib/dcidev_active_record.rb
CHANGED
@@ -2,136 +2,121 @@ require 'active_support/concern'
|
|
2
2
|
require 'active_record'
|
3
3
|
|
4
4
|
module DcidevActiveRecord
|
5
|
-
|
6
|
-
|
7
|
-
included do
|
8
|
-
scope :between_date, -> (column, start_date, end_date) {
|
9
|
-
where("#{eval("self.#{ENV['DB']}_date_builder('#{self.table_name}.#{column}')")} BETWEEN '#{start_date}' AND '#{end_date}'")
|
10
|
-
}
|
11
|
-
|
12
|
-
scope :before_or_equal_to_date, -> (column, date) {
|
13
|
-
where("#{eval("self.#{ENV['DB']}_date_builder('#{self.table_name}.#{column}')")} <= '#{date}'")
|
14
|
-
}
|
5
|
+
extend ActiveSupport::Concern
|
15
6
|
|
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
|
-
eval("self.#{c} = params[:#{c.to_sym}]") if params.key?(c.to_sym)
|
46
|
-
eval("self.#{c} = params['#{c}']") if params.key?(c)
|
47
|
-
else
|
48
|
-
eval("self.#{c} = params[:#{c.to_sym}]") if params.key?(c.to_sym) && params[c.to_sym] != nil
|
49
|
-
eval("self.#{c} = params['#{c}']") if params.key?(c) && params[c] != nil
|
50
|
-
end
|
51
|
-
rescue IOError
|
52
|
-
raise "Tidak dapat menyimpan file#{c}"
|
53
|
-
end
|
7
|
+
included do
|
8
|
+
scope :between_date, -> (column, start_date, end_date) {
|
9
|
+
where("#{eval("self.#{ENV['DB']}_date_builder('#{self.table_name}.#{column}')")} BETWEEN '#{start_date}' AND '#{end_date}'")
|
10
|
+
}
|
11
|
+
|
12
|
+
scope :before_or_equal_to_date, -> (column, date) {
|
13
|
+
where("#{eval("self.#{ENV['DB']}_date_builder('#{self.table_name}.#{column}')")} <= '#{date}'")
|
14
|
+
}
|
15
|
+
|
16
|
+
scope :after_or_equal_to_date, -> (column, date) {
|
17
|
+
where("#{eval("self.#{ENV['DB']}_date_builder('#{self.table_name}.#{column}')")} >= '#{date}'")
|
18
|
+
}
|
19
|
+
scope :at_time, -> (column, time) {
|
20
|
+
where("#{eval("self.#{ENV['DB']}_time_builder('#{self.table_name}.#{column}')")} #{self.db_like_string(ENV['DB'])} '%#{time}%'")
|
21
|
+
}
|
22
|
+
scope :mysql_json_contains, ->(column, key, value) { "JSON_EXTRACT(#{column}, '$.\"#{key}\"') LIKE \"%#{value}%\"" }
|
23
|
+
end
|
24
|
+
|
25
|
+
def replace_child_from_array(new_child = [], column_name: "", child_name: "")
|
26
|
+
new_child = new_child.to_a
|
27
|
+
formatted = []
|
28
|
+
existing_child = eval("self.#{child_name}")
|
29
|
+
existing_child_values = existing_child.pluck(column_name.to_sym)
|
30
|
+
delete_child = existing_child_values - new_child
|
31
|
+
(existing_child_values + new_child).uniq.each do |lc|
|
32
|
+
attr = { column_name => lc, "_destroy" => delete_child.include?(lc) }
|
33
|
+
id = existing_child.detect { |ec| eval("ec.#{column_name} == #{lc.is_a?(String) ? "'#{lc}'" : lc}") }
|
34
|
+
attr["id"] = id.id if id.present?
|
35
|
+
formatted << attr
|
54
36
|
end
|
55
|
-
|
56
|
-
|
37
|
+
formatted
|
38
|
+
end
|
39
|
+
|
40
|
+
def update_by_params(params, set_nil = true)
|
41
|
+
ActiveRecord::Base.transaction do
|
42
|
+
self.class.column_names.each do |c|
|
43
|
+
begin
|
44
|
+
if set_nil
|
45
|
+
eval("self.#{c} = params[:#{c.to_sym}]") if params.key?(c.to_sym)
|
46
|
+
eval("self.#{c} = params['#{c}']") if params.key?(c)
|
47
|
+
else
|
48
|
+
eval("self.#{c} = params[:#{c.to_sym}]") if params.key?(c.to_sym) && params[c.to_sym] != nil
|
49
|
+
eval("self.#{c} = params['#{c}']") if params.key?(c) && params[c] != nil
|
50
|
+
end
|
51
|
+
rescue IOError
|
52
|
+
raise "Tidak dapat menyimpan file#{c}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
params.select { |k, _| !k.is_a?(Symbol) && k.include?("_attributes") }.each do |k, _|
|
56
|
+
eval("self.#{k} = params[:#{k.to_sym}]")
|
57
|
+
end
|
58
|
+
self.save
|
57
59
|
end
|
58
|
-
self.save
|
59
|
-
end
|
60
60
|
end
|
61
|
-
|
62
|
-
|
61
|
+
|
63
62
|
def set_order
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
def reorder
|
74
|
-
return unless self.class.column_names.include?("view_order")
|
75
|
-
return unless self.class.where(view_order: self.view_order).where.not(id: self.id).present?
|
76
|
-
self.class.order(view_order: :asc, updated_at: :desc).each.with_index(1) do |f, i|
|
77
|
-
f.update(view_order: i)
|
78
|
-
end
|
63
|
+
return unless self.class.column_names.include?("view_order")
|
64
|
+
if self.view_order.present?
|
65
|
+
self.reorder
|
66
|
+
else
|
67
|
+
self.view_order = self.class.where.not(id: self.id).count + 1
|
68
|
+
self.save
|
69
|
+
end
|
79
70
|
end
|
80
|
-
|
81
|
-
class_methods do
|
82
71
|
|
83
|
-
def
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
eval("model.#{c} = params['#{c}']") if params.key?(c)
|
89
|
-
|
90
|
-
rescue IOError
|
91
|
-
raise "Tidak dapat menyimpan file #{c}"
|
72
|
+
def reorder
|
73
|
+
return unless self.class.column_names.include?("view_order")
|
74
|
+
return unless self.class.where(view_order: self.view_order).where.not(id: self.id).present?
|
75
|
+
self.class.order(view_order: :asc, updated_at: :desc).each.with_index(1) do |f, i|
|
76
|
+
f.update(view_order: i)
|
92
77
|
end
|
93
|
-
end
|
94
|
-
params.select{|k, _| !k.is_a?(Symbol) && k.include?("_attributes")}.each do |k, _|
|
95
|
-
eval("model.#{k} = params[:#{k.to_sym}]")
|
96
|
-
end
|
97
|
-
model
|
98
|
-
end
|
99
|
-
|
100
|
-
def mysql_date_builder(field)
|
101
|
-
"DATE(CONVERT_TZ(#{field}, '+00:00', '#{Time.now.in_time_zone(Time.zone.name.to_s).formatted_offset}'))"
|
102
|
-
end
|
103
|
-
|
104
|
-
def mysql_time_builder(field)
|
105
|
-
"TIME(CONVERT_TZ(#{field}, '+00:00', '#{Time.now.in_time_zone(Time.zone.name.to_s).formatted_offset}'))"
|
106
|
-
end
|
107
|
-
|
108
|
-
def pgsql_date_builder(field)
|
109
|
-
"DATE(#{field}::TIMESTAMPTZ AT TIME ZONE '#{Time.zone.now.formatted_offset}'::INTERVAL)"
|
110
|
-
end
|
111
|
-
|
112
|
-
def pgsql_time_builder(field)
|
113
|
-
"#{field}::TIMESTAMPTZ AT TIME ZONE '#{Time.zone.now.formatted_offset}'"
|
114
78
|
end
|
115
79
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
attr.each do |child|
|
128
|
-
children << r.classify.constantize.find_or_initialize_by(child)
|
80
|
+
class_methods do
|
81
|
+
|
82
|
+
def new_from_params(params)
|
83
|
+
model = self.new
|
84
|
+
self.column_names.each do |c|
|
85
|
+
begin
|
86
|
+
eval("model.#{c} = params[:#{c.to_sym}]") if params.key?(c.to_sym)
|
87
|
+
eval("model.#{c} = params['#{c}']") if params.key?(c)
|
88
|
+
|
89
|
+
rescue IOError
|
90
|
+
raise "Tidak dapat menyimpan file #{c}"
|
129
91
|
end
|
130
|
-
eval("self.#{r} = children")
|
131
92
|
end
|
93
|
+
params.select { |k, _| !k.is_a?(Symbol) && k.include?("_attributes") }.each do |k, _|
|
94
|
+
eval("model.#{k} = params[:#{k.to_sym}]")
|
95
|
+
end
|
96
|
+
model
|
97
|
+
end
|
98
|
+
|
99
|
+
def mysql_date_builder(field)
|
100
|
+
"DATE(CONVERT_TZ(#{field}, '+00:00', '#{Time.now.in_time_zone(Time.zone.name.to_s).formatted_offset}'))"
|
101
|
+
end
|
102
|
+
|
103
|
+
def mysql_time_builder(field)
|
104
|
+
"TIME(CONVERT_TZ(#{field}, '+00:00', '#{Time.now.in_time_zone(Time.zone.name.to_s).formatted_offset}'))"
|
105
|
+
end
|
106
|
+
|
107
|
+
def pgsql_date_builder(field)
|
108
|
+
"DATE(#{field}::TIMESTAMPTZ AT TIME ZONE '#{Time.zone.now.formatted_offset}'::INTERVAL)"
|
109
|
+
end
|
110
|
+
|
111
|
+
def pgsql_time_builder(field)
|
112
|
+
"#{field}::TIMESTAMPTZ AT TIME ZONE '#{Time.zone.now.formatted_offset}'"
|
113
|
+
end
|
114
|
+
|
115
|
+
def db_like_string(db)
|
116
|
+
return 'ILIKE' if db == 'pgsql'
|
117
|
+
return 'LIKE' if db == 'mysql'
|
132
118
|
end
|
133
119
|
end
|
134
120
|
end
|
135
|
-
end
|
136
121
|
|
137
122
|
ActiveRecord::Base.send(:include, DcidevActiveRecord)
|