dcidev_active_record 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +1 -0
  3. data/lib/dcidev_active_record.rb +109 -0
  4. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8241a63b734ee3eb8ca4198bb50a2d22243da122d516c8305d4a90c3172b2a13
4
+ data.tar.gz: 3d3cabdb63b8bd0a52fc4eaf1813787a62cb91b027004ea74330e582299fa744
5
+ SHA512:
6
+ metadata.gz: 679948d3dfe60825c43ce736e4ac7fe2a4e0bdd539bae0d44d68e58d25a19e0d7e57e7e210650b7bf051b68b7a0b1abb6883949fd965104168888014933715d1
7
+ data.tar.gz: 4de885d42e3b81dba5cd97add3601335838a4fcb412049da2c33cc26529fa3a525b5a27050ce3ab2037b7edc070742065b143ef57c7e13e21885599f8917cbdc
data/README.md ADDED
@@ -0,0 +1 @@
1
+ tambakan ENV['DB'] = mysql / postgresql
@@ -0,0 +1,109 @@
1
+ require 'active_support/concern'
2
+ require 'active_record'
3
+
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
+ }
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
+
26
+ def update_by_params(params, set_nil = true)
27
+ ActiveRecord::Base.transaction do
28
+ self.class.column_names.each do |c|
29
+ begin
30
+ if set_nil
31
+ eval("self.#{c} = params[:#{c.to_sym}]") if params.key?(c.to_sym)
32
+ eval("self.#{c} = params['#{c}']") if params.key?(c)
33
+ else
34
+ eval("self.#{c} = params[:#{c.to_sym}]") if params.key?(c.to_sym) && params[c.to_sym] != nil
35
+ eval("self.#{c} = params['#{c}']") if params.key?(c) && params[c] != nil
36
+ end
37
+ rescue IOError
38
+ raise "Tidak dapat menyimpan file#{c}"
39
+ end
40
+ end
41
+ params.select{|k, _| !k.is_a?(Symbol) && k.include?("_attributes")}.each do |k, _|
42
+ eval("self.#{k} = params[:#{k.to_sym}]")
43
+ end
44
+ self.save
45
+ end
46
+ end
47
+
48
+
49
+ def set_order
50
+ return unless self.class.column_names.include?("view_order")
51
+ if self.view_order.present?
52
+ self.reorder
53
+ else
54
+ self.view_order = self.class.where.not(id: self.id).count + 1
55
+ self.save
56
+ end
57
+ end
58
+
59
+ def reorder
60
+ return unless self.class.column_names.include?("view_order")
61
+ return unless self.class.where(view_order: self.view_order).where.not(id: self.id).present?
62
+ self.class.order(view_order: :asc, updated_at: :desc).each.with_index(1) do |f, i|
63
+ f.update(view_order: i)
64
+ end
65
+ end
66
+
67
+ class_methods do
68
+
69
+ def new_from_params(params)
70
+ model = self.new
71
+ self.column_names.each do |c|
72
+ begin
73
+ eval("model.#{c} = params[:#{c.to_sym}]") if params.key?(c.to_sym)
74
+ eval("model.#{c} = params['#{c}']") if params.key?(c)
75
+
76
+ rescue IOError
77
+ raise "Tidak dapat menyimpan file #{c}"
78
+ end
79
+ end
80
+ params.select{|k, _| !k.is_a?(Symbol) && k.include?("_attributes")}.each do |k, _|
81
+ eval("model.#{k} = params[:#{k.to_sym}]")
82
+ end
83
+ model
84
+ end
85
+
86
+ def mysql_date_builder(field)
87
+ "DATE(CONVERT_TZ(#{field}, '+00:00', '#{Time.now.in_time_zone(Time.zone.name.to_s).formatted_offset}'))"
88
+ end
89
+
90
+ def mysql_time_builder(field)
91
+ "TIME(CONVERT_TZ(#{field}, '+00:00', '#{Time.now.in_time_zone(Time.zone.name.to_s).formatted_offset}'))"
92
+ end
93
+
94
+ def postgresql_date_builder(field)
95
+ "DATE(#{field}::TIMESTAMPTZ AT TIME ZONE '#{Time.zone.now.formatted_offset}'::INTERVAL)"
96
+ end
97
+
98
+ def postgresql_time_builder(field)
99
+ "#{field}::TIMESTAMPTZ AT TIME ZONE '#{Time.zone.now.formatted_offset}'"
100
+ end
101
+
102
+ def db_like_string(db)
103
+ return 'ILIKE' if db == 'postgresql'
104
+ return 'LIKE' if db == 'mysql'
105
+ end
106
+ end
107
+ end
108
+
109
+ ActiveRecord::Base.send(:include, DcidevActiveRecord)
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dcidev_active_record
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ platform: ruby
6
+ authors:
7
+ - Punto Damar P
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-01-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Testing phase
14
+ email:
15
+ - punto@privyid.tech
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - lib/dcidev_active_record.rb
22
+ homepage:
23
+ licenses: []
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.0.6
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: This gem extends ActiveRecord features
44
+ test_files: []