activerecord-enum_sti 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2864a486385e91036a6ea2b553456718d9c3314cc27bbabf677d834f0d8cf965
4
+ data.tar.gz: bc8cd160397908527f5feb5fae2b2dace8c3fc5cf2a4b297fe7842f9def2154f
5
+ SHA512:
6
+ metadata.gz: 7dbe099d17b9af52ca7e7fd85709fabdc020e24894a2dd386756d449e599ab4651be32772e5f44ca347d499e9e51c05024239b86b84a5199d0df9723a5aef6e0
7
+ data.tar.gz: f069c2eb9f5204040c159199a0b02dfe5815c28147b49745e6133bba9ce2151200f461f1ed63a4dc2ddb431fa6b14e3b4c4e77246a976eb607255b956652bbb1
@@ -0,0 +1,37 @@
1
+ name: CI
2
+ on: [push]
3
+ jobs:
4
+ test:
5
+ runs-on: ubuntu-latest
6
+ strategy:
7
+ matrix:
8
+ ruby:
9
+ - '2.5'
10
+ - '2.6'
11
+ - '2.7'
12
+ rails:
13
+ - 'rails_5.2'
14
+ - 'rails_6.0'
15
+ - 'rails_edge'
16
+ steps:
17
+ - name: Checkout
18
+ uses: actions/checkout@v1
19
+ - name: Setup System
20
+ run: |
21
+ sudo apt-get install libsqlite3-dev
22
+ - name: Setup Ruby
23
+ uses: actions/setup-ruby@v1
24
+ with:
25
+ ruby-version: ${{ matrix.ruby }}
26
+ - name: Bundle
27
+ env:
28
+ MTSR_RAILS_VERSION: ${{ matrix.rails }}
29
+ run: |
30
+ export BUNDLE_GEMFILE="${GITHUB_WORKSPACE}/gemfiles/${MTSR_RAILS_VERSION}.gemfile"
31
+ bundle install --jobs 4 --retry 3
32
+ - name: Test
33
+ env:
34
+ MTSR_RAILS_VERSION: ${{ matrix.rails }}
35
+ run: |
36
+ export BUNDLE_GEMFILE="${GITHUB_WORKSPACE}/gemfiles/${MTSR_RAILS_VERSION}.gemfile"
37
+ bundle exec rake
@@ -0,0 +1,18 @@
1
+ name: release
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - v*
7
+ jobs:
8
+ build:
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - uses: actions/checkout@v1
12
+ - name: Release Gem
13
+ if: contains(github.ref, 'refs/tags/v')
14
+ uses: cadwallion/publish-rubygems-action@master
15
+ env:
16
+ GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
17
+ RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}}
18
+ RELEASE_COMMAND: rake release
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ vendor
14
+ *.gemfile.lock
15
+ gemfiles/.bundle
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at ksuzuki180@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
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 activerecord-enum_sti.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 suusan2go
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.
@@ -0,0 +1,125 @@
1
+ # ActiveaRecord::EnumSti ![CI](https://github.com/suusan2go/activerecord-enum_sti/workflows/CI/badge.svg)
2
+
3
+ ActiveRecord::EnumSti is a library to use enum values as STI type.
4
+
5
+ When you use enum, you might see the code like following.
6
+
7
+ ```ruby
8
+ class Payment < ApplicationRecord
9
+ enum kind: { credit_card: 0, bank_transfer: 10, paypal: 20 }
10
+
11
+ def complete!
12
+ if credit_card?
13
+ # do something
14
+ elsif bank_transfer?
15
+ # do something
16
+ elsif paypal?
17
+ # do something
18
+ end
19
+ end
20
+
21
+ def cancel!
22
+ if credit_card?
23
+ # do something
24
+ elsif bank_transfer?
25
+ # do something
26
+ elsif paypal?
27
+ # do something
28
+ end
29
+ end
30
+ end
31
+ ```
32
+
33
+ In this code, we change the behavior according to the value of enum.
34
+ But if we can change the class by the value of enum, this code will be more clear.
35
+
36
+ This gem allows you to split the class using the Single Table Inheritance (STI) in Rails depending on the value of the enum.
37
+
38
+ ```ruby
39
+ class Payment < ApplicationRecord
40
+ include ActiveRecord::EnumSti
41
+ enum type: { credit_card: 0, bank_transfer: 10, paypal: 20 }
42
+
43
+ def complete!
44
+ raise NotImplementedError
45
+ end
46
+
47
+ def cancel!
48
+ raise NotImplementedError
49
+ end
50
+ end
51
+
52
+ class Payment::CreditCard < Payment
53
+ def complete!
54
+ # do something for creditcard!
55
+ end
56
+
57
+ def cancel!
58
+ # do something for creditcard!
59
+ end
60
+ end
61
+ ```
62
+
63
+ ## Installation
64
+
65
+ Add this line to your application's Gemfile:
66
+
67
+ ```ruby
68
+ gem 'activerecord-enum_sti'
69
+ ```
70
+
71
+ And then execute:
72
+
73
+ $ bundle
74
+
75
+ Or install it yourself as:
76
+
77
+ $ gem install activerecord-enum_sti
78
+
79
+ ## Usage
80
+ First of all, include `ActiveRecord::EnumSti` in your enum defined class.
81
+
82
+ ```ruby
83
+ class Payment < ApplicationRecord
84
+ include ActiveRecord::EnumSti
85
+ enum type: { credit_card: 0, bank_transfer: 10, paypal: 20 }
86
+ # if you want to use the another column, you can use another enum column by below.
87
+ # self.inheritance_column = :your_enum_column
88
+ end
89
+ ```
90
+
91
+ Then, please define a class for each enum values. The class name should be `<SuperClass>::<camelized enum value>`
92
+
93
+ ```ruby
94
+ class Payment::CreditCard < Payment
95
+ end
96
+ ```
97
+
98
+ Now, you can use the value of enum for STI like below!
99
+
100
+ ```
101
+ @credit_card = Payment::CreditCard.create
102
+ @credit_card.type
103
+ => "credit_card"
104
+ Payment::CreditCard.all.to_sql
105
+ => "SELECT \"payments\".* FROM \"payments\" WHERE \"payments\".\"type\" = 0"
106
+ ```
107
+
108
+
109
+ ## Development
110
+
111
+ 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.
112
+
113
+ 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).
114
+
115
+ ## Contributing
116
+
117
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/activerecord-enum_sti. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
118
+
119
+ ## License
120
+
121
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
122
+
123
+ ## Code of Conduct
124
+
125
+ Everyone interacting in the Activerecord::EnumSti project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/activerecord-enum_sti/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,45 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "active_record/enum_sti/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord-enum_sti"
8
+ spec.version = ActiveRecord::EnumSti::VERSION
9
+ spec.authors = ["suusan2go"]
10
+ spec.email = ["ksuzuki180@gmail.com"]
11
+
12
+ spec.summary = %q{Use enum value as a STI type}
13
+ spec.description = %q{This gem allows you to use enum value as a STI type value easily.}
14
+ spec.homepage = "https://github.com/suusan2go/activerecord-enum_sti"
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
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://github.com/suusan2go/activerecord-enum_sti"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_development_dependency "bundler"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "rspec", "~> 3.0"
41
+ spec.add_development_dependency 'sqlite3'
42
+
43
+ spec.add_runtime_dependency 'activesupport'
44
+ spec.add_runtime_dependency 'activerecord', '>= 5.2'
45
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "activerecord/enum_sti"
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__)
@@ -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
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activerecord", github: "rails/rails", branch: "5-2-stable"
4
+
5
+ gemspec :path => "../"
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activerecord", github: "rails/rails", branch: "6-0-stable"
4
+
5
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ git "https://github.com/rails/rails.git", :branch => "master" do
4
+ gem "activerecord"
5
+ end
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,31 @@
1
+ require "active_record/enum_sti/version"
2
+ require "active_support/concern"
3
+ require "active_support/core_ext/string/inflections"
4
+
5
+ module ActiveRecord
6
+ module EnumSti
7
+ extend ActiveSupport::Concern
8
+
9
+ class Error < StandardError; end
10
+
11
+ class_methods do
12
+ def find_sti_class(type)
13
+ type_name = enum_items.key(type)
14
+ prefix = if superclass == ActiveRecord::Base || superclass.abstract_class
15
+ name
16
+ else
17
+ superclass.name
18
+ end
19
+ "#{prefix}::#{type_name.to_s.camelize}".constantize
20
+ end
21
+
22
+ def sti_name
23
+ enum_items[name.demodulize.underscore]
24
+ end
25
+
26
+ def enum_items
27
+ public_send(inheritance_column.pluralize)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module EnumSti
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-enum_sti
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - suusan2go
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-08-28 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '5.2'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '5.2'
97
+ description: This gem allows you to use enum value as a STI type value easily.
98
+ email:
99
+ - ksuzuki180@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".github/workflows/ci.yaml"
105
+ - ".github/workflows/release.yaml"
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - activerecord-enum_sti.gemspec
114
+ - bin/console
115
+ - bin/setup
116
+ - gemfiles/rails_5.2.gemfile
117
+ - gemfiles/rails_6.0.gemfile
118
+ - gemfiles/rails_edge.gemfile
119
+ - lib/active_record/enum_sti.rb
120
+ - lib/active_record/enum_sti/version.rb
121
+ homepage: https://github.com/suusan2go/activerecord-enum_sti
122
+ licenses:
123
+ - MIT
124
+ metadata:
125
+ allowed_push_host: https://rubygems.org
126
+ homepage_uri: https://github.com/suusan2go/activerecord-enum_sti
127
+ source_code_uri: https://github.com/suusan2go/activerecord-enum_sti
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubygems_version: 3.0.3
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Use enum value as a STI type
147
+ test_files: []