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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39ef130c33dba54d43cb868680de1a87e6a21fdd0aa1ec784231b0faad38e998
4
- data.tar.gz: 51bb74f205875eba8875bf1136e8c53ae4b61ef0bf7587224603e809f477ea0b
3
+ metadata.gz: 8446ee19d66a28093f5a5944f61d386cce63842d56c7290eaac3e6e7ccc96341
4
+ data.tar.gz: 7e0e82c4d8fe0bb5496680f272412ac620832982e9eee9987f76dd77d4e9a5aa
5
5
  SHA512:
6
- metadata.gz: f6be9964e2b1fee13e235bb9c6ca21dfc9fb2e472cb425ef9afe47e1d2175194c93138b2965c02fca6276ed1a7dd45e9f6b17fc797952cb2044c671888d3f20d
7
- data.tar.gz: 6d5d16730a92f8b577ae7b1d181cd888842590778e74156e292450b9954c1fed0954b6d60c6300b112b4a08fbde85de5922ae6835039f81f3f8a5dbf7f7facae
6
+ metadata.gz: 8faab31f65faafe8aec947c52d8b0574e213aba2914929a5f147d265a6d5f4f16bc5f334ad3dd647566350d8e06e57120af20d345ea4a7289c114b4b8337402c
7
+ data.tar.gz: d764ee8a95bfe93b77cdba33a271739c425c7fc17af3a78946659320a4e54a15f6b350a832041c52ee44142efafcd1869902d5cc27ba42c08c7fe598d639175d
@@ -2,136 +2,121 @@ require 'active_support/concern'
2
2
  require 'active_record'
3
3
 
4
4
  module DcidevActiveRecord
5
- extend ActiveSupport::Concern
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
- 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
36
- end
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
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
- params.select{|k, _| !k.is_a?(Symbol) && k.include?("_attributes")}.each do |k, _|
56
- eval("self.#{k} = params[:#{k.to_sym}]")
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
- return unless self.class.column_names.include?("view_order")
65
- if self.view_order.present?
66
- self.reorder
67
- else
68
- self.view_order = self.class.where.not(id: self.id).count + 1
69
- self.save
70
- end
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 new_from_params(params)
84
- model = self.new
85
- self.column_names.each do |c|
86
- begin
87
- eval("model.#{c} = params[:#{c.to_sym}]") if params.key?(c.to_sym)
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
- def db_like_string(db)
117
- return 'ILIKE' if db == 'pgsql'
118
- return 'LIKE' if db == 'mysql'
119
- end
120
- end
121
-
122
- def init_nested_attributes(reflections)
123
- reflections.each do |r|
124
- define_method("#{r}_attributes=") do |attr|
125
- if attr.present?
126
- children = []
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.10
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: {}