dcidev_active_record 0.0.7 → 0.0.10

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 +4 -4
  2. data/README.md +41 -1
  3. data/lib/dcidev_active_record.rb +31 -3
  4. metadata +7 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8241a63b734ee3eb8ca4198bb50a2d22243da122d516c8305d4a90c3172b2a13
4
- data.tar.gz: 3d3cabdb63b8bd0a52fc4eaf1813787a62cb91b027004ea74330e582299fa744
3
+ metadata.gz: 39ef130c33dba54d43cb868680de1a87e6a21fdd0aa1ec784231b0faad38e998
4
+ data.tar.gz: 51bb74f205875eba8875bf1136e8c53ae4b61ef0bf7587224603e809f477ea0b
5
5
  SHA512:
6
- metadata.gz: 679948d3dfe60825c43ce736e4ac7fe2a4e0bdd539bae0d44d68e58d25a19e0d7e57e7e210650b7bf051b68b7a0b1abb6883949fd965104168888014933715d1
7
- data.tar.gz: 4de885d42e3b81dba5cd97add3601335838a4fcb412049da2c33cc26529fa3a525b5a27050ce3ab2037b7edc070742065b143ef57c7e13e21885599f8917cbdc
6
+ metadata.gz: f6be9964e2b1fee13e235bb9c6ca21dfc9fb2e472cb425ef9afe47e1d2175194c93138b2965c02fca6276ed1a7dd45e9f6b17fc797952cb2044c671888d3f20d
7
+ data.tar.gz: 6d5d16730a92f8b577ae7b1d181cd888842590778e74156e292450b9954c1fed0954b6d60c6300b112b4a08fbde85de5922ae6835039f81f3f8a5dbf7f7facae
data/README.md CHANGED
@@ -1 +1,41 @@
1
- tambakan ENV['DB'] = mysql / postgresql
1
+ # How to Use
2
+ add `DB= mysql / postgresql` to your `.env` file
3
+
4
+ # Features
5
+
6
+ ### Query Scope Filtering
7
+ ```ruby
8
+ model.between_date(column, start_date, end_date)
9
+ model.before_or_equal_to_date(date, date)
10
+ model.after_or_equal_to_date(column, date)
11
+ model.at_time(column, time)
12
+ model.mysql_json_contains(column, key, value)
13
+ ```
14
+
15
+ ### Class Method
16
+ ```ruby
17
+ # initialize using hash, the model is not saved yet
18
+ Model.new_from_params(params)
19
+
20
+ # convert from UTC to server date
21
+ Model.mysql_date_builder(column)
22
+
23
+ # convert from UTC to server time
24
+ Model.mysql_time_builder(column)
25
+
26
+ # same as above but for postgresql
27
+ Model.postgresql_date_builder(column)
28
+ Model.postgresql_time_builder(column)
29
+ ```
30
+
31
+ ### Instance Method
32
+ ```ruby
33
+ # update a record using hash parameter
34
+ # if set_nil = true, the unspesified column will be automatically be set to nil
35
+ model.update_by_params(params, set_nil = true)
36
+
37
+ # available for one-many relationship
38
+ # replace all child of this model with specified value from array
39
+ # set model accepts_nested_attributes_for :child,allow_destroy: true
40
+ model.replace_child_from_array(new_child = [], column_name: "", child_name: "")
41
+ ```
@@ -22,6 +22,20 @@ module DcidevActiveRecord
22
22
  scope :mysql_json_contains, ->(column, key, value) {"JSON_EXTRACT(#{column}, '$.\"#{key}\"') LIKE \"%#{value}%\""}
23
23
  end
24
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
25
39
 
26
40
  def update_by_params(params, set_nil = true)
27
41
  ActiveRecord::Base.transaction do
@@ -91,19 +105,33 @@ module DcidevActiveRecord
91
105
  "TIME(CONVERT_TZ(#{field}, '+00:00', '#{Time.now.in_time_zone(Time.zone.name.to_s).formatted_offset}'))"
92
106
  end
93
107
 
94
- def postgresql_date_builder(field)
108
+ def pgsql_date_builder(field)
95
109
  "DATE(#{field}::TIMESTAMPTZ AT TIME ZONE '#{Time.zone.now.formatted_offset}'::INTERVAL)"
96
110
  end
97
111
 
98
- def postgresql_time_builder(field)
112
+ def pgsql_time_builder(field)
99
113
  "#{field}::TIMESTAMPTZ AT TIME ZONE '#{Time.zone.now.formatted_offset}'"
100
114
  end
101
115
 
102
116
  def db_like_string(db)
103
- return 'ILIKE' if db == 'postgresql'
117
+ return 'ILIKE' if db == 'pgsql'
104
118
  return 'LIKE' if db == 'mysql'
105
119
  end
106
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)
129
+ end
130
+ eval("self.#{r} = children")
131
+ end
132
+ end
133
+ end
134
+ end
107
135
  end
108
136
 
109
137
  ActiveRecord::Base.send(:include, DcidevActiveRecord)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dcidev_active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Punto Damar P
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-05 00:00:00.000000000 Z
11
+ date: 2022-05-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Testing phase
14
14
  email:
@@ -19,10 +19,10 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - README.md
21
21
  - lib/dcidev_active_record.rb
22
- homepage:
22
+ homepage:
23
23
  licenses: []
24
24
  metadata: {}
25
- post_install_message:
25
+ post_install_message:
26
26
  rdoc_options: []
27
27
  require_paths:
28
28
  - lib
@@ -37,8 +37,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
37
  - !ruby/object:Gem::Version
38
38
  version: '0'
39
39
  requirements: []
40
- rubygems_version: 3.0.6
41
- signing_key:
40
+ rubygems_version: 3.0.3.1
41
+ signing_key:
42
42
  specification_version: 4
43
43
  summary: This gem extends ActiveRecord features
44
44
  test_files: []