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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dcidev_active_record.rb +101 -116
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 016dea935c8d62a764c1b5488347d03199304974da1942e868189d446e4ac99e
4
- data.tar.gz: 62e529217307601d18c3ba0d341ccb90d25e600d50ee13ce7bffdb1706a78850
3
+ metadata.gz: 81d43d6c23726bfcc5dd25fb8d4cfe17e30ba7f7a11a2c12f7999926e7e7bcae
4
+ data.tar.gz: b66a24c0c256baf7eb1912e19d574c133e3b0b009340db9d90d59e95b7615d52
5
5
  SHA512:
6
- metadata.gz: '0886cfce0c6b68ad19035839121a950e578f3a682e84b6b3e986dbdbdbbe64dcbb625cf0f283d8fe44dc82d4528602ac0d2431ceedd7e080c6a4f8928488d8f9'
7
- data.tar.gz: 70cd0e8ce9ed0ec43393d36e68b4b8c9410b0299cb2ae90373a9c320130c19754f01abc7d97b9f81b7a6843bee3a5507f5a37e3563c8f49f6ccd65fb3b69886a
6
+ metadata.gz: 971004a905d4f940e0ea2649ec22262410577570ca051d59d2b72f3a11f5df06471ae97e6953d355609dabfc7789819d0ca24a011a9a8b72d71d3417bdc46d65
7
+ data.tar.gz: 450e364d5d0cbc5aa5ed671097cf323ef7cda6d5a518a0debfd05a3309e1500bd24ec807745d1b0c04f7b1713d904ab2e26743293d75d9d9185d7b2cfe0e78d3
@@ -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)
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.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Punto Damar P