dropkiq 0.1.5 → 0.1.12

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
- SHA1:
3
- metadata.gz: b29aa33cb5696a6b569e4d7db891b71d8b554551
4
- data.tar.gz: 33f3a5353cd519dbf1577aaed5e8a2738f39755b
2
+ SHA256:
3
+ metadata.gz: 3263d78f3e1091246733f08a91a94939c30bbdfaa4f04bb2f2fc5c78a8df2fc9
4
+ data.tar.gz: f06abae5d94474fb36fa2f85b640a795be8db9435e2bb46b89202ed7739c2d1a
5
5
  SHA512:
6
- metadata.gz: cf739265bf1d2fac61eef26c0d5ed00a700e2ee9fe8f4597abfeac118e7f3eba3b9165f8fac5c3bd9b0c754cf554bc1ad926c6a410a5979172af4c9f1a407ae2
7
- data.tar.gz: 8a3cd96c060f04f75a16f67c2b3a917715f4996015987d4330e653b782dc365666cf47ae3465e061f7b38d0fe7d3f99ab5d9f9822c651b14d209ce07ce154e38
6
+ metadata.gz: 1f5c246119d1704a2235ce9c1f49b37db9383ff037b2acd8b118528951a19df0cbf460099460ed32f89ad81cfa9e7bc0d963d35d77c45db21c7c266d0f2c9699
7
+ data.tar.gz: '01138583a8ecdd63d3c88cf5cf53139b2dc35178dc8023343ca9455b6e70225d45e55f20c56ba8b4c7e135b3de40cb4f5a1f4977e5c1ca15be0cbdae6585f8c5'
@@ -1,27 +1,26 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dropkiq (0.1.5)
4
+ dropkiq (0.1.11)
5
5
  activerecord (>= 4.2)
6
6
  liquid (~> 4.0)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activemodel (5.2.4.1)
12
- activesupport (= 5.2.4.1)
13
- activerecord (5.2.4.1)
14
- activemodel (= 5.2.4.1)
15
- activesupport (= 5.2.4.1)
16
- arel (>= 9.0)
17
- activesupport (5.2.4.1)
11
+ activemodel (6.0.3.1)
12
+ activesupport (= 6.0.3.1)
13
+ activerecord (6.0.3.1)
14
+ activemodel (= 6.0.3.1)
15
+ activesupport (= 6.0.3.1)
16
+ activesupport (6.0.3.1)
18
17
  concurrent-ruby (~> 1.0, >= 1.0.2)
19
18
  i18n (>= 0.7, < 2)
20
19
  minitest (~> 5.1)
21
20
  tzinfo (~> 1.1)
22
- arel (9.0.0)
21
+ zeitwerk (~> 2.2, >= 2.2.2)
23
22
  coderay (1.1.2)
24
- concurrent-ruby (1.1.5)
23
+ concurrent-ruby (1.1.6)
25
24
  i18n (1.8.2)
26
25
  concurrent-ruby (~> 1.0)
27
26
  liquid (4.0.3)
@@ -33,11 +32,12 @@ GEM
33
32
  pry (0.12.2)
34
33
  coderay (~> 1.1.0)
35
34
  method_source (~> 0.9.0)
36
- rake (10.5.0)
35
+ rake (13.0.1)
37
36
  sqlite3 (1.3.13)
38
37
  thread_safe (0.3.6)
39
- tzinfo (1.2.6)
38
+ tzinfo (1.2.7)
40
39
  thread_safe (~> 0.1)
40
+ zeitwerk (2.3.0)
41
41
 
42
42
  PLATFORMS
43
43
  ruby
@@ -49,7 +49,7 @@ DEPENDENCIES
49
49
  minitest-focus (~> 1.1.2)
50
50
  mocha (~> 1.11.2)
51
51
  pry (~> 0.12.2)
52
- rake (~> 10.0)
52
+ rake (~> 13.0)
53
53
  sqlite3 (~> 1.3.13)
54
54
 
55
55
  BUNDLED WITH
data/README.md CHANGED
@@ -30,7 +30,7 @@ Or install it yourself as:
30
30
 
31
31
  This Gem makes several assumptions about your Ruby on Rails application:
32
32
 
33
- * Drop classes are expected to have the same name as the corresponding Rails model. For example, if you have an ActiveRecord model with name of `Person`, the drop class is expected to be called `PersonDrop`
33
+ * Drop classes are expected to have the same name as the corresponding Rails model. For example, if you have an ActiveRecord model with name of `Person`, the drop class is expected to be called `PersonDrop` (Ruby on Rails applications using the legacy `liquid_methods` method with [PR #568](https://github.com/Shopify/liquid/pull/568) are also supported.)
34
34
  * Drop class methods are expected to return the same data type as the corresponding Rails-model getter methods for relationships and columns.
35
35
  * This Gem has not been tested with Rails models that have non-default primary key (other than `id`).
36
36
 
@@ -44,7 +44,7 @@ bundle exec rake dropkiq:schema
44
44
 
45
45
  You should now have a `db/dropkiq_schema.yaml` file. This file describes all tables associated to Drop classes in your application, along with all methods avaialble to the Drop class. It is important that you **DO** allow this file to be checked in to version control so that you can maintain your Dropkiq schema over time as you add/remove/modify your Drop classes.
46
46
 
47
- Notice that `type` has NOT been set on all methods. The Dropkiq Gem is only able to infer the method type for methods that correspond to a column or relationship on the ActiveRecord model. Please take a moment to manually add `type` values for all methods that were not able to be inferred. Notice that if you run `bundle exec rake dropkiq:schema` again, your changes are saved!
47
+ Notice that `type` has NOT been set on all methods. The Dropkiq Gem is only able to infer the method type for methods that correspond to a column or relationship on the ActiveRecord model. Please take a moment to manually add `type` values for all methods that were not able to be inferred. (Hint: Search the dropkiq_schema.yaml file for the word "CHANGEME" to see what needs manual attention.) Notice that if you run `bundle exec rake dropkiq:schema` again, your changes are saved!
48
48
 
49
49
 
50
50
  #### Ruby on Rails Column to Dropkiq Data Type Mappings
@@ -122,6 +122,45 @@ class ProductDrop < Liquid::Drop
122
122
  end
123
123
  ```
124
124
 
125
+ #### Dropkiq Method Classification
126
+
127
+ In the event that a liquid method is not an exact match to a column or association, the Dropkiq Ruby Gem will attempt to make a best guess about the data type of the method based on the name. The rules for matching are as follows:
128
+
129
+ | Method Name Ends With | Dropkiq Data Type |
130
+ | --- | --- |
131
+ | `_id` | `ColumnTypes::Numeric` |
132
+ | `_count` | `ColumnTypes::Numeric` |
133
+ | `?` | `ColumnTypes::Boolean` |
134
+ | `_present` | `ColumnTypes::Boolean` |
135
+ | `_changed` | `ColumnTypes::Boolean` |
136
+ | `description` | `ColumnTypes::Text` |
137
+ | `name` | `ColumnTypes::String` |
138
+ | `password` | `ColumnTypes::String` |
139
+ | `type` | `ColumnTypes::String` |
140
+ | `title` | `ColumnTypes::String` |
141
+ | `to_s` | `ColumnTypes::String` |
142
+ | `to_string` | `ColumnTypes::String` |
143
+ | `_url` | `ColumnTypes::String` |
144
+ | `_email` | `ColumnTypes::String` |
145
+ | `_partial` | `ColumnTypes::String` |
146
+ | `_email_address` | `ColumnTypes::String` |
147
+ | `_uuid` | `ColumnTypes::String` |
148
+
149
+ The Dropkiq Ruby Gem will also attempt to classify liquid methods by creating a sample instance of the Liquid Drop class to test. The liquid method will be called, and the value will attempt to be classified using the foollowing:
150
+
151
+ | Value Result | Dropkiq Data Type |
152
+ | --- | --- |
153
+ | `TrueClass` | `ColumnTypes::Boolean` |
154
+ | `FalseClass` | `ColumnTypes::Boolean` |
155
+ | `String` | `ColumnTypes::String` |
156
+ | `Symbol` | `ColumnTypes::String` |
157
+ | `Numeric` | `ColumnTypes::Numeric` |
158
+ | `Date` | `ColumnTypes::DateTime` |
159
+ | `Time` | `ColumnTypes::DateTime` |
160
+ | `DateTime` | `ColumnTypes::DateTime` |
161
+
162
+ Dropkiq will attempt to classify relationships in the event the method returns a single ActiveRecord object (`ColumnTypes::HasOne`), or a collection or ActiveRecord objects (`ColumnTypes::HasMany`).
163
+
125
164
  ## Development
126
165
 
127
166
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -38,7 +38,7 @@ Gem::Specification.new do |spec|
38
38
 
39
39
  spec.add_development_dependency "pry", "~> 0.12.2"
40
40
  spec.add_development_dependency "bundler", "~> 2.0"
41
- spec.add_development_dependency "rake", "~> 10.0"
41
+ spec.add_development_dependency "rake", "~> 13.0"
42
42
  spec.add_development_dependency "minitest", "~> 5.0"
43
43
  spec.add_development_dependency "sqlite3", "~> 1.3.13"
44
44
  spec.add_development_dependency "mocha", "~> 1.11.2"
@@ -1,4 +1,3 @@
1
- require "pry"
2
1
  require "liquid"
3
2
  require 'active_record'
4
3
 
@@ -7,6 +6,8 @@ require "dropkiq/constants"
7
6
 
8
7
  require "dropkiq/drop_class_analyzer"
9
8
  require "dropkiq/drop_method_analyzer"
9
+ require "dropkiq/drop_method_name_classifier"
10
+ require "dropkiq/drop_method_instance_simulator"
10
11
  require 'dropkiq/railtie' if defined?(Rails)
11
12
 
12
13
  module Dropkiq
@@ -1,5 +1,6 @@
1
1
  module Dropkiq
2
2
  DEFAULT_DROP_PATH = "/app/drops"
3
+ DEFAULT_MODEL_PATH = "/app/models"
3
4
 
4
5
  DEFAULT_LIQUID_DROP_CLASSES = [
5
6
  Liquid::ForloopDrop,
@@ -12,7 +12,7 @@ module Dropkiq
12
12
  self.active_record_class ||= find_active_record_class_from_shim
13
13
 
14
14
  if active_record_class.blank?
15
- puts "WARNING: No ActiveRecord Class found for #{liquid_drop_class.name}"
15
+ puts "WARNING: No ActiveRecord Class found for #{liquid_drop_class.name} (skipping)"
16
16
  return
17
17
  end
18
18
 
@@ -52,8 +52,14 @@ module Dropkiq
52
52
  default_methods = (Liquid::Drop.instance_methods + Object.instance_methods)
53
53
  instance_methods = (liquid_drop_class.instance_methods - default_methods)
54
54
 
55
+ sample_instance = active_record_class.first
56
+ sample_drop = begin
57
+ liquid_drop_class.new(sample_instance)
58
+ rescue
59
+ end
60
+
55
61
  instance_methods.inject({}) do |hash, method|
56
- analyzer = Dropkiq::DropMethodAnalyzer.new(self, method)
62
+ analyzer = Dropkiq::DropMethodAnalyzer.new(self, method, sample_drop)
57
63
  analyzer.analyze
58
64
  hash.merge!(analyzer.to_param)
59
65
  end.sort_by { |key| key }.to_h
@@ -1,13 +1,16 @@
1
1
  module Dropkiq
2
2
  class DropMethodAnalyzer
3
+ CHANGEME = "CHANGEME"
4
+
3
5
  attr_accessor :drop_class_analyzer, :drop_method,
4
- :dropkiq_type, :foreign_table_name
6
+ :dropkiq_type, :foreign_table_name, :sample_drop
5
7
 
6
8
  delegate :active_record_class, to: :drop_class_analyzer
7
9
 
8
- def initialize(drop_class_analyzer, drop_method)
10
+ def initialize(drop_class_analyzer, drop_method, sample_drop=nil)
9
11
  self.drop_class_analyzer = drop_class_analyzer
10
12
  self.drop_method = drop_method
13
+ self.sample_drop = sample_drop
11
14
  end
12
15
 
13
16
  def analyze
@@ -15,13 +18,21 @@ module Dropkiq
15
18
  relationship_to_dropkiq_type_classifier
16
19
  elsif is_column?
17
20
  column_to_dropkiq_type_classifier
21
+ else
22
+ Dropkiq::DropMethodNameClassifier.new(drop_method).classify
23
+ end
24
+
25
+ if dropkiq_type.blank?
26
+ result = Dropkiq::DropMethodInstanceSimulator.new(drop_method, sample_drop).classify
27
+ self.dropkiq_type = result.dropkiq_type
28
+ self.foreign_table_name = result.foreign_table_name
18
29
  end
19
30
  end
20
31
 
21
32
  def to_param
22
33
  {
23
34
  "#{drop_method}" => {
24
- "type" => dropkiq_type,
35
+ "type" => (dropkiq_type.presence || CHANGEME),
25
36
  "foreign_table_name" => foreign_table_name
26
37
  }
27
38
  }
@@ -43,8 +54,8 @@ module Dropkiq
43
54
  begin
44
55
  self.foreign_table_name = reflection.class_name.constantize.table_name
45
56
  rescue
46
- puts "Table not found for reflect_on_association value: #{reflection.inspect}"
47
- raise
57
+ puts "WARNING: Could not find #{drop_method} on #{active_record_class.name}"
58
+ return
48
59
  end
49
60
 
50
61
  case reflection
@@ -0,0 +1,58 @@
1
+ module Dropkiq
2
+ class DropMethodInstanceSimulator
3
+ attr_accessor :drop_method, :sample_drop
4
+ attr_accessor :dropkiq_type, :foreign_table_name
5
+
6
+ def initialize(drop_method, sample_drop=nil)
7
+ self.drop_method = drop_method.to_s
8
+ self.sample_drop = sample_drop
9
+ end
10
+
11
+ def classify
12
+ value = begin
13
+ sample_drop.try(drop_method)
14
+ rescue
15
+ end
16
+
17
+ self.dropkiq_type = ruby_data_type_to_dropkiq_type(value)
18
+ self.dropkiq_type ||= test_for_relationship(value)
19
+
20
+ self
21
+ end
22
+
23
+ private
24
+
25
+ def test_for_relationship(value)
26
+ if value.is_a?(ActiveRecord::Base)
27
+ self.foreign_table_name = value.class.table_name
28
+ return Dropkiq::HAS_ONE_TYPE
29
+ elsif value.respond_to?(:first) && value.first.is_a?(ActiveRecord::Base)
30
+ self.foreign_table_name = value.first.class.table_name
31
+ return Dropkiq::HAS_MANY_TYPE
32
+ end
33
+ end
34
+
35
+ def ruby_data_type_to_dropkiq_type(value)
36
+ case value
37
+ when NilClass
38
+ when TrueClass
39
+ Dropkiq::BOOLEAN_TYPE
40
+ when FalseClass
41
+ Dropkiq::BOOLEAN_TYPE
42
+ when String
43
+ Dropkiq::STRING_TYPE
44
+ when Symbol
45
+ Dropkiq::STRING_TYPE
46
+ when Numeric
47
+ Dropkiq::NUMERIC_TYPE
48
+ when Date
49
+ Dropkiq::DATE_TIME_TYPE
50
+ when Time
51
+ Dropkiq::DATE_TIME_TYPE
52
+ when DateTime
53
+ Dropkiq::DATE_TIME_TYPE
54
+ else
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,52 @@
1
+ module Dropkiq
2
+ class DropMethodNameClassifier
3
+ attr_accessor :drop_method
4
+
5
+ def initialize(drop_method)
6
+ self.drop_method = drop_method.to_s
7
+ end
8
+
9
+ def classify
10
+ if numeric_type_match?
11
+ Dropkiq::NUMERIC_TYPE
12
+ elsif boolean_type_match?
13
+ Dropkiq::BOOLEAN_TYPE
14
+ elsif text_type_match?
15
+ Dropkiq::TEXT_TYPE
16
+ elsif string_type_match?
17
+ Dropkiq::STRING_TYPE
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def numeric_type_match?
24
+ drop_method.ends_with?("_id") ||
25
+ drop_method.ends_with?("_count")
26
+ end
27
+
28
+ def boolean_type_match?
29
+ drop_method.ends_with?("?") ||
30
+ drop_method.ends_with?("_present") ||
31
+ drop_method.ends_with?("_changed")
32
+ end
33
+
34
+ def text_type_match?
35
+ drop_method.ends_with?("description")
36
+ end
37
+
38
+ def string_type_match?
39
+ drop_method.ends_with?("name") ||
40
+ drop_method.ends_with?("password") ||
41
+ drop_method.ends_with?("type") ||
42
+ drop_method.ends_with?("title") ||
43
+ drop_method.ends_with?("to_s") ||
44
+ drop_method.ends_with?("to_string") ||
45
+ drop_method.ends_with?("_url") ||
46
+ drop_method.ends_with?("_email") ||
47
+ drop_method.ends_with?("_partial") ||
48
+ drop_method.ends_with?("_email_address") ||
49
+ drop_method.ends_with?("_uuid")
50
+ end
51
+ end
52
+ end
@@ -1,9 +1,23 @@
1
+ require 'stringio'
2
+
1
3
  namespace :dropkiq do
2
4
  desc "Generate the fixture schema based on Liquid::Drop classes"
3
5
  task :schema do
4
6
  require "#{Rails.root}/config/environment.rb"
7
+
5
8
  Dir.glob("#{Rails.root}#{Dropkiq::DEFAULT_DROP_PATH}/**/*.rb").each { |f| load f }
6
9
 
10
+ # http://alphahydrae.com/2013/09/capturing-output-in-pure-ruby/
11
+ stdout, stderr = StringIO.new, StringIO.new
12
+ $stdout, $stderr = stdout, stderr
13
+ Dir.glob("#{Rails.root}#{Dropkiq::DEFAULT_MODEL_PATH}/**/*.rb").each do |f|
14
+ begin
15
+ load f
16
+ rescue
17
+ end
18
+ end
19
+ $stdout, $stderr = STDOUT, STDERR
20
+
7
21
  existing_schema_yaml = begin
8
22
  File.read("#{Rails.root}/db/dropkiq_schema.yaml")
9
23
  rescue Errno::ENOENT
@@ -1,3 +1,3 @@
1
1
  module Dropkiq
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.12"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dropkiq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Darrah
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-20 00:00:00.000000000 Z
11
+ date: 2020-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '13.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '13.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: minitest
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -160,6 +160,8 @@ files:
160
160
  - lib/dropkiq/constants.rb
161
161
  - lib/dropkiq/drop_class_analyzer.rb
162
162
  - lib/dropkiq/drop_method_analyzer.rb
163
+ - lib/dropkiq/drop_method_instance_simulator.rb
164
+ - lib/dropkiq/drop_method_name_classifier.rb
163
165
  - lib/dropkiq/railtie.rb
164
166
  - lib/dropkiq/tasks/dropkiq/schema.rake
165
167
  - lib/dropkiq/version.rb
@@ -182,8 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
182
184
  - !ruby/object:Gem::Version
183
185
  version: '0'
184
186
  requirements: []
185
- rubyforge_project:
186
- rubygems_version: 2.6.14.1
187
+ rubygems_version: 3.0.3
187
188
  signing_key:
188
189
  specification_version: 4
189
190
  summary: Integrate your Ruby on Rails application with Dropkiq for easy Liquid editing