dcidev_active_record 0.0.10 → 0.0.13
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
- data/lib/nested_attribute_helper.rb +19 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8446ee19d66a28093f5a5944f61d386cce63842d56c7290eaac3e6e7ccc96341
|
4
|
+
data.tar.gz: 7e0e82c4d8fe0bb5496680f272412ac620832982e9eee9987f76dd77d4e9a5aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8faab31f65faafe8aec947c52d8b0574e213aba2914929a5f147d265a6d5f4f16bc5f334ad3dd647566350d8e06e57120af20d345ea4a7289c114b4b8337402c
|
7
|
+
data.tar.gz: d764ee8a95bfe93b77cdba33a271739c425c7fc17af3a78946659320a4e54a15f6b350a832041c52ee44142efafcd1869902d5cc27ba42c08c7fe598d639175d
|
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)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module NestedAttributeHelper
|
4
|
+
def init_nested_attributes(reflections)
|
5
|
+
reflections.each do |r|
|
6
|
+
define_method("#{r}_attributes=") do |attr|
|
7
|
+
if attr.present?
|
8
|
+
children = []
|
9
|
+
attr.each do |child|
|
10
|
+
children << r.classify.constantize.find_or_initialize_by(child)
|
11
|
+
end
|
12
|
+
eval("self.#{r} = children")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
ActiveRecord::Base.send(:extend, NestedAttributeHelper)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dcidev_active_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Punto Damar P
|
@@ -19,6 +19,7 @@ extra_rdoc_files: []
|
|
19
19
|
files:
|
20
20
|
- README.md
|
21
21
|
- lib/dcidev_active_record.rb
|
22
|
+
- lib/nested_attribute_helper.rb
|
22
23
|
homepage:
|
23
24
|
licenses: []
|
24
25
|
metadata: {}
|