dropkiq 0.1.6 → 0.1.13

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: 7325e9f9615a3dd0c4ebb7f1d867f40bb240e05e
4
- data.tar.gz: 7397de313f3ed61d656a765310483260b145e24b
2
+ SHA256:
3
+ metadata.gz: 950efeb574c42b0103eeaaf86a980c88cbbe661f8e62b5065a0458ef3c2c0290
4
+ data.tar.gz: 93c8cb4258ba14394c959a77d9e6119e62a7ceba5742d386610002aa6d6d2860
5
5
  SHA512:
6
- metadata.gz: f07fb2afe3e42b4b58e2d7ff38d61e7b1ae345db1399b0fffcaa6c245c3bd625c1dbafa67da1316d0d1c545f8c73cff1e9cd2d8399d4944b676faf1fe1a38ed3
7
- data.tar.gz: 90bfd0ff73c3ef824300ffb61acecce5e4971254dddc460c8796285d5d00ddb40d2992d21f4f3a585c1ddf535b0b5ff960562ea9cb89c29c6e7d8095fdbed08e
6
+ metadata.gz: 3fdd64185ddcb76f686852af1c4a1333b5304a7e0eef42f0d3a663f7f5e6eada14f2640f8b6095af6701fa9ac9c0ea30923b31caaaa067bc54dd319b3373862a
7
+ data.tar.gz: 8e786e90553a1ce32cc24b74b52fcd7a2c420b1ac951f1d3273f052a562ae36d72d602dfa06f7c411d5eb7709cd29bbafb5cf3e49eb9c4321b17524b405cd2b1
@@ -1,28 +1,27 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dropkiq (0.1.6)
4
+ dropkiq (0.1.13)
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.4)
12
+ activesupport (= 6.0.3.4)
13
+ activerecord (6.0.3.4)
14
+ activemodel (= 6.0.3.4)
15
+ activesupport (= 6.0.3.4)
16
+ activesupport (6.0.3.4)
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)
25
- i18n (1.8.2)
23
+ concurrent-ruby (1.1.7)
24
+ i18n (1.8.5)
26
25
  concurrent-ruby (~> 1.0)
27
26
  liquid (4.0.3)
28
27
  method_source (0.9.2)
@@ -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.8)
40
39
  thread_safe (~> 0.1)
40
+ zeitwerk (2.4.2)
41
41
 
42
42
  PLATFORMS
43
43
  ruby
@@ -49,8 +49,8 @@ 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
56
- 2.0.1
56
+ 2.1.4
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,8 +6,14 @@ 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
 
13
+ Dir[File.join(File.dirname(__FILE__), 'tasks', '**/*.rake')].each do |rake|
14
+ load rake
15
+ end
16
+
12
17
  module Dropkiq
13
18
  class Error < StandardError; end
14
19
  end
@@ -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,
@@ -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,15 +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
- return {} if dropkiq_type.blank?
23
-
24
33
  {
25
34
  "#{drop_method}" => {
26
- "type" => dropkiq_type,
35
+ "type" => (dropkiq_type.presence || CHANGEME),
27
36
  "foreign_table_name" => foreign_table_name
28
37
  }
29
38
  }
@@ -45,7 +54,7 @@ module Dropkiq
45
54
  begin
46
55
  self.foreign_table_name = reflection.class_name.constantize.table_name
47
56
  rescue
48
- puts "WARNING: Could not find #{drop_method} on #{active_record_class.name} (skipping)"
57
+ puts "WARNING: Could not find #{drop_method} on #{active_record_class.name}"
49
58
  return
50
59
  end
51
60
 
@@ -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.6"
2
+ VERSION = "0.1.13"
3
3
  end
@@ -0,0 +1,12 @@
1
+ # https://coderwall.com/p/qhdhgw/adding-a-post-execution-hook-to-the-rails-db-migrate-task
2
+
3
+ namespace :db do
4
+ def run_dropkiq_schema
5
+ puts "\nRunning `bundle exec rake dropkiq:schema` ...\n\n"
6
+ Rake::Task['dropkiq:schema'].invoke
7
+ end
8
+
9
+ task :migrate do
10
+ run_dropkiq_schema
11
+ end
12
+ 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.6
4
+ version: 0.1.13
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-12-11 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,9 +160,12 @@ 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
168
+ - lib/tasks/dropkiq_models_migrate.rake
166
169
  homepage: https://github.com/akdarrah/dropkiq-gem
167
170
  licenses:
168
171
  - MIT
@@ -182,8 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
182
185
  - !ruby/object:Gem::Version
183
186
  version: '0'
184
187
  requirements: []
185
- rubyforge_project:
186
- rubygems_version: 2.6.14.1
188
+ rubygems_version: 3.0.3
187
189
  signing_key:
188
190
  specification_version: 4
189
191
  summary: Integrate your Ruby on Rails application with Dropkiq for easy Liquid editing