dcidev_active_record 0.0.8 → 0.0.11

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: 1f162b561542bc46075a729b74e1a12a030a60f30b427d89d65d7edf2020e7e6
4
- data.tar.gz: 7a48a57c3d0f6fb8c78cf6ef8a035d9159c4555b30451cf9e93d1987f63651a9
3
+ metadata.gz: 016dea935c8d62a764c1b5488347d03199304974da1942e868189d446e4ac99e
4
+ data.tar.gz: 62e529217307601d18c3ba0d341ccb90d25e600d50ee13ce7bffdb1706a78850
5
5
  SHA512:
6
- metadata.gz: 0a8a8c0aa82a3d1dd37e35bd67920c052f0e223caf02842792231921cb5091889fda528214fb52beeb7469ce3ac42960811bc540f95f5dc76572540031a750db
7
- data.tar.gz: 2f1be735bd55d21fc8563d59e17782913065383fa8bea9fb60231153348dc5ea783d65a10908cef2c9c3c1986c5dbe8d7ad270d425f4db588c52de09b6f398c9
6
+ metadata.gz: '0886cfce0c6b68ad19035839121a950e578f3a682e84b6b3e986dbdbdbbe64dcbb625cf0f283d8fe44dc82d4528602ac0d2431ceedd7e080c6a4f8928488d8f9'
7
+ data.tar.gz: 70cd0e8ce9ed0ec43393d36e68b4b8c9410b0299cb2ae90373a9c320130c19754f01abc7d97b9f81b7a6843bee3a5507f5a37e3563c8f49f6ccd65fb3b69886a
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
+ ```
@@ -105,19 +105,33 @@ module DcidevActiveRecord
105
105
  "TIME(CONVERT_TZ(#{field}, '+00:00', '#{Time.now.in_time_zone(Time.zone.name.to_s).formatted_offset}'))"
106
106
  end
107
107
 
108
- def postgresql_date_builder(field)
108
+ def pgsql_date_builder(field)
109
109
  "DATE(#{field}::TIMESTAMPTZ AT TIME ZONE '#{Time.zone.now.formatted_offset}'::INTERVAL)"
110
110
  end
111
111
 
112
- def postgresql_time_builder(field)
112
+ def pgsql_time_builder(field)
113
113
  "#{field}::TIMESTAMPTZ AT TIME ZONE '#{Time.zone.now.formatted_offset}'"
114
114
  end
115
115
 
116
116
  def db_like_string(db)
117
- return 'ILIKE' if db == 'postgresql'
117
+ return 'ILIKE' if db == 'pgsql'
118
118
  return 'LIKE' if db == 'mysql'
119
119
  end
120
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
121
135
  end
122
136
 
123
137
  ActiveRecord::Base.send(:include, DcidevActiveRecord)
@@ -0,0 +1,14 @@
1
+ module NestedAttributeHelper
2
+ def init_nested_attributes(reflections)
3
+ reflections.each do |r|
4
+ define_method("#{r}_attributes=") do |attr|
5
+ if attr.present?
6
+ children = []
7
+ attr.each do |child|
8
+ children << r.classify.constantize.find_or_initialize_by(child)
9
+ end
10
+ eval("self.#{r} = children")
11
+ end
12
+ end
13
+ end
14
+ end
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.8
4
+ version: 0.0.11
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-07 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,11 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - README.md
21
21
  - lib/dcidev_active_record.rb
22
- homepage:
22
+ - lib/nested_attribute_helper.rb
23
+ homepage:
23
24
  licenses: []
24
25
  metadata: {}
25
- post_install_message:
26
+ post_install_message:
26
27
  rdoc_options: []
27
28
  require_paths:
28
29
  - lib
@@ -37,8 +38,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
38
  - !ruby/object:Gem::Version
38
39
  version: '0'
39
40
  requirements: []
40
- rubygems_version: 3.2.3
41
- signing_key:
41
+ rubygems_version: 3.0.3.1
42
+ signing_key:
42
43
  specification_version: 4
43
44
  summary: This gem extends ActiveRecord features
44
45
  test_files: []