antecedent 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fb8a23077c978e531dd2d3dce8283ede814f78f3
4
+ data.tar.gz: d999291bbddb20760175b4072fff75f2b2f467b4
5
+ SHA512:
6
+ metadata.gz: cf78057b6c284066901da4a869dc5f8bc6026cb78f058ca10fc0ca2afbb0a753737b8a3aaec64d9854cb99083f48336ee2bf43a1165ff89782f8659f7bd8745f
7
+ data.tar.gz: f494b9f2a14f8e375fba42f055133483d27f28b5cf825350a71eb2a3dd2be22dca0cba264c6241f5ad929ec52b7b756c1992877f5212da6f8e600e2deb41a5f2
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in antecedent.gemspec
6
+ gemspec
7
+
8
+ group :test do
9
+ gem 'activerecord', '~> 5.2'
10
+ gem 'pg', '~> 1.0'
11
+ gem 'standalone_migrations', '~> 5.2', '>= 5.2.5'
12
+ gem 'database_cleaner', '~> 1.6', '>= 1.6.2'
13
+ gem 'factory_bot', '~> 4.8', '>= 4.8.2'
14
+ gem 'pry', '~> 0.11.3'
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,111 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ antecedent (0.1.0)
5
+ activerecord (>= 4.2.7, < 5.3.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (5.2.0)
11
+ actionview (= 5.2.0)
12
+ activesupport (= 5.2.0)
13
+ rack (~> 2.0)
14
+ rack-test (>= 0.6.3)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
17
+ actionview (5.2.0)
18
+ activesupport (= 5.2.0)
19
+ builder (~> 3.1)
20
+ erubi (~> 1.4)
21
+ rails-dom-testing (~> 2.0)
22
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
23
+ activemodel (5.2.0)
24
+ activesupport (= 5.2.0)
25
+ activerecord (5.2.0)
26
+ activemodel (= 5.2.0)
27
+ activesupport (= 5.2.0)
28
+ arel (>= 9.0)
29
+ activesupport (5.2.0)
30
+ concurrent-ruby (~> 1.0, >= 1.0.2)
31
+ i18n (>= 0.7, < 2)
32
+ minitest (~> 5.1)
33
+ tzinfo (~> 1.1)
34
+ arel (9.0.0)
35
+ builder (3.2.3)
36
+ coderay (1.1.2)
37
+ concurrent-ruby (1.0.5)
38
+ crass (1.0.4)
39
+ database_cleaner (1.6.2)
40
+ diff-lcs (1.3)
41
+ erubi (1.7.1)
42
+ factory_bot (4.8.2)
43
+ activesupport (>= 3.0.0)
44
+ i18n (1.0.0)
45
+ concurrent-ruby (~> 1.0)
46
+ loofah (2.2.2)
47
+ crass (~> 1.0.2)
48
+ nokogiri (>= 1.5.9)
49
+ method_source (0.9.0)
50
+ mini_portile2 (2.3.0)
51
+ minitest (5.11.3)
52
+ nokogiri (1.8.2)
53
+ mini_portile2 (~> 2.3.0)
54
+ pg (1.0.0)
55
+ pry (0.11.3)
56
+ coderay (~> 1.1.0)
57
+ method_source (~> 0.9.0)
58
+ rack (2.0.4)
59
+ rack-test (1.0.0)
60
+ rack (>= 1.0, < 3)
61
+ rails-dom-testing (2.0.3)
62
+ activesupport (>= 4.2.0)
63
+ nokogiri (>= 1.6)
64
+ rails-html-sanitizer (1.0.4)
65
+ loofah (~> 2.2, >= 2.2.2)
66
+ railties (5.2.0)
67
+ actionpack (= 5.2.0)
68
+ activesupport (= 5.2.0)
69
+ method_source
70
+ rake (>= 0.8.7)
71
+ thor (>= 0.18.1, < 2.0)
72
+ rake (10.5.0)
73
+ rspec (3.7.0)
74
+ rspec-core (~> 3.7.0)
75
+ rspec-expectations (~> 3.7.0)
76
+ rspec-mocks (~> 3.7.0)
77
+ rspec-core (3.7.1)
78
+ rspec-support (~> 3.7.0)
79
+ rspec-expectations (3.7.0)
80
+ diff-lcs (>= 1.2.0, < 2.0)
81
+ rspec-support (~> 3.7.0)
82
+ rspec-mocks (3.7.0)
83
+ diff-lcs (>= 1.2.0, < 2.0)
84
+ rspec-support (~> 3.7.0)
85
+ rspec-support (3.7.1)
86
+ standalone_migrations (5.2.5)
87
+ activerecord (>= 4.2.7, < 5.3.0)
88
+ railties (>= 4.2.7, < 5.3.0)
89
+ rake (>= 10.0)
90
+ thor (0.20.0)
91
+ thread_safe (0.3.6)
92
+ tzinfo (1.2.5)
93
+ thread_safe (~> 0.1)
94
+
95
+ PLATFORMS
96
+ ruby
97
+
98
+ DEPENDENCIES
99
+ activerecord (~> 5.2)
100
+ antecedent!
101
+ bundler (~> 1.16)
102
+ database_cleaner (~> 1.6, >= 1.6.2)
103
+ factory_bot (~> 4.8, >= 4.8.2)
104
+ pg (~> 1.0)
105
+ pry (~> 0.11.3)
106
+ rake (~> 10.0)
107
+ rspec (~> 3.0)
108
+ standalone_migrations (~> 5.2, >= 5.2.5)
109
+
110
+ BUNDLED WITH
111
+ 1.16.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Wontae Yang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # Antecedent
2
+
3
+ In some cases it is not desirable to retrieve STI
4
+ classes in polymorphic relation and having scopes to
5
+ filter by type column is good enough. (ex: I had to create
6
+ a read only client API app that retrieves records
7
+ from existing Rails DB). This gem was created to
8
+ override default behavior of ActiveRecord
9
+ `BelongsToPolymorphicAssociation`
10
+ and return association in its base classes.
11
+
12
+ It supports polymorphic type columns with base class names (ex: "User")
13
+ and full class name (ex: "User::Admin").
14
+
15
+ TODO: Add support for other AR versions (current version is only for 5.2)
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'antecedent'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install antecedent
32
+
33
+ ## Usage
34
+
35
+ to disable entirely add following line in `config/initializers/antecedent.rb`:
36
+
37
+ ```
38
+ Antecedent.disable_sti
39
+ ```
40
+
41
+ using a wrapper method:
42
+
43
+ ```
44
+ def without_sti
45
+ Antecedent.disable_sti
46
+ yield
47
+ Antecedent.enable_sti
48
+ end
49
+
50
+ without_sti do
51
+ #your code here
52
+ end
53
+ ```
54
+
55
+ to allow STI in some models while disabled, add following:
56
+
57
+ ```
58
+ class User < ActiveRecord::Base
59
+ self.inheritance_column = :type
60
+ end
61
+ ```
62
+
63
+ ## Development
64
+
65
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
66
+
67
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
68
+
69
+ ## Test
70
+
71
+ setup test db
72
+
73
+ ```
74
+ rake db:setup db:migrate RAILS_ENV=test
75
+ ```
76
+
77
+ run spec:
78
+
79
+ ```
80
+ rspec .
81
+ ```
82
+
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/antecedent.
87
+
88
+ ## License
89
+
90
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ require 'standalone_migrations'
9
+ StandaloneMigrations::Tasks.load_tasks
@@ -0,0 +1,39 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "antecedent/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "antecedent"
8
+ spec.version = Antecedent::VERSION
9
+ spec.authors = ["Wontae Yang"]
10
+ spec.email = ["wontaeyang@gmail.com"]
11
+
12
+ spec.summary = %q{Override STI in polymorphic relation.}
13
+ spec.description = %q{Sometimes, it is not desirable to retrieve STI models. This gem will allow relation to be returned in parent class.}
14
+ spec.homepage = "https://github.com/wontaeyang/antecedent"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.16"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+
37
+ # Gem dependency
38
+ spec.add_dependency("activerecord", ["< 5.3.0", ">= 4.2.7"])
39
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "antecedent"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/db/config.yml ADDED
@@ -0,0 +1,9 @@
1
+ default: &default
2
+ adapter: postgresql
3
+ encoding: unicode
4
+ pool: 5
5
+
6
+ test:
7
+ <<: *default
8
+ database: antecedent_test
9
+
@@ -0,0 +1,10 @@
1
+ class CreateUsers < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :users do |t|
4
+ t.string :name
5
+ t.string :type
6
+ t.integer :parent_id
7
+ t.string :parent_type
8
+ end
9
+ end
10
+ end
data/db/schema.rb ADDED
@@ -0,0 +1,25 @@
1
+ # This file is auto-generated from the current state of the database. Instead
2
+ # of editing this file, please use the migrations feature of Active Record to
3
+ # incrementally modify your database, and then regenerate this schema definition.
4
+ #
5
+ # Note that this schema.rb definition is the authoritative source for your
6
+ # database schema. If you need to create the application database on another
7
+ # system, you should be using db:schema:load, not running all the migrations
8
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
9
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
10
+ #
11
+ # It's strongly recommended that you check this file into your version control system.
12
+
13
+ ActiveRecord::Schema.define(version: 2018_04_10_014636) do
14
+
15
+ # These are extensions that must be enabled in order to support this database
16
+ enable_extension "plpgsql"
17
+
18
+ create_table "users", force: :cascade do |t|
19
+ t.string "name"
20
+ t.string "type"
21
+ t.integer "parent_id"
22
+ t.string "parent_type"
23
+ end
24
+
25
+ end
data/lib/antecedent.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "antecedent/version"
2
+ require "active_record"
3
+
4
+ if ActiveRecord::VERSION::STRING =~ /^5\./
5
+ require "antecedent/active_record_5"
6
+ else
7
+ require "antecednt/fallback"
8
+ end
9
+
@@ -0,0 +1,36 @@
1
+ module Antecedent
2
+ ASSOCIATION = ActiveRecord::Associations::BelongsToPolymorphicAssociation
3
+
4
+ def self.disable_sti
5
+ ASSOCIATION.class_eval do
6
+ alias _klass klass
7
+
8
+ def klass
9
+ type = owner[reflection.foreign_type]
10
+ type.presence && type_base_class(type)
11
+ end
12
+
13
+ private
14
+
15
+ def type_base_class(type)
16
+ type.constantize.base_class
17
+ rescue NameError
18
+ # if type class does not exist try
19
+ # removing namespace
20
+ type.deconstantize.constantize
21
+ end
22
+ end
23
+
24
+ ActiveRecord::Base.inheritance_column = :_type_disabled
25
+ end
26
+
27
+ def self.enable_sti
28
+ ASSOCIATION.class_eval do
29
+ if defined?(_klass)
30
+ alias klass _klass
31
+ end
32
+ end
33
+
34
+ ActiveRecord::Base.inheritance_column = :type
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ module Antecedent
2
+ def self.disable_sti
3
+ raise_version_error
4
+ end
5
+
6
+ def self.enable_sti
7
+ raise_version_error
8
+ end
9
+
10
+ private
11
+
12
+ def self.raise_version_error
13
+ raise Antecedent::ActiveRecordVersionNotSupported.new(
14
+ "ActiveRecord version not supported"
15
+ )
16
+ end
17
+ end
18
+
19
+ class Antecedent::ActiveRecordVersionNotSupported < StandardError;end
@@ -0,0 +1,3 @@
1
+ module Antecedent
2
+ VERSION = "0.1.0"
3
+ end
data/tags ADDED
@@ -0,0 +1,8 @@
1
+ !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
2
+ !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
3
+ !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/
4
+ !_TAG_PROGRAM_NAME Exuberant Ctags //
5
+ !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/
6
+ !_TAG_PROGRAM_VERSION 5.8 //
7
+ Antecedent lib/antecedent.rb /^module Antecedent$/;" m
8
+ Antecedent lib/antecedent/version.rb /^module Antecedent$/;" m
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: antecedent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Wontae Yang
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "<"
60
+ - !ruby/object:Gem::Version
61
+ version: 5.3.0
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 4.2.7
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "<"
70
+ - !ruby/object:Gem::Version
71
+ version: 5.3.0
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 4.2.7
75
+ description: Sometimes, it is not desirable to retrieve STI models. This gem will
76
+ allow relation to be returned in parent class.
77
+ email:
78
+ - wontaeyang@gmail.com
79
+ executables: []
80
+ extensions: []
81
+ extra_rdoc_files: []
82
+ files:
83
+ - ".gitignore"
84
+ - ".rspec"
85
+ - ".travis.yml"
86
+ - Gemfile
87
+ - Gemfile.lock
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - antecedent.gemspec
92
+ - bin/console
93
+ - bin/setup
94
+ - db/config.yml
95
+ - db/migrate/20180410014636_create_users.rb
96
+ - db/schema.rb
97
+ - lib/antecedent.rb
98
+ - lib/antecedent/active_record_5.rb
99
+ - lib/antecedent/fallback.rb
100
+ - lib/antecedent/version.rb
101
+ - tags
102
+ homepage: https://github.com/wontaeyang/antecedent
103
+ licenses:
104
+ - MIT
105
+ metadata:
106
+ allowed_push_host: https://rubygems.org
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.5.1
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Override STI in polymorphic relation.
127
+ test_files: []