has_sti 0.0.1

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: e2d316dd54b23149479449315066fed5a78baf1f
4
+ data.tar.gz: dc37b2589c3d96ea91695e4d2a98a6624c5a80fc
5
+ SHA512:
6
+ metadata.gz: cb3ee2e0ffb1ac0ce42f02b85ebdf300e53a17d82172ef33115ab5b4fd2b3770e59976ac9557f3e56082f0de90ce47c3667424a3edfbc0d662621641b77d1a63
7
+ data.tar.gz: f9d4e63687cb690fbba747accb0b8880bcae9b9328fa7199b2b00ffb98aa5f4e75f5a4259f28c1d4a1bfdceff4ad2e6a20e8eecb734f2962df43a092f1541483
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Arkadiusz Fal
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,58 @@
1
+ {<img src="https://travis-ci.org/arekf/has_sti.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/arekf/has_sti]
2
+
3
+ = has_sti
4
+
5
+ +has_sti+ is an ActiveRecord extension that provides helper methods and scopes for STI models.
6
+
7
+ == Installation
8
+ * Add +has_sti+ to your Gemfile:
9
+ gem 'has_sti'
10
+ * Install with:
11
+ bundle install
12
+
13
+ == Usage
14
+ We will use Ruby on Rails for this scenario. Let's say +User+ model is a parent STI class and you want to create child classes: +Admin+ and +Reader+. You start with defining STI as follows:
15
+
16
+ class User < ActiveRecord::Base
17
+ end
18
+
19
+ class Admin < User
20
+ end
21
+
22
+ class Reader < User
23
+ end
24
+
25
+ Next step is creating migration for the database:
26
+ rails generate migration AddTypeToUser type:string
27
+
28
+ Then, we need to migrate database:
29
+ bundle exec rake db:migrate
30
+
31
+ Basic setup of STI is done. Here's where +has_sti+ takes action.
32
+
33
+ Modify your +User+ model to include +has_sti+:
34
+
35
+ class User < ActiveRecord::Base
36
+ has_sti :admin, :reader
37
+ end
38
+
39
+ And that's it. Now you have following methods and scopes available:
40
+ some_admin = Admin.create
41
+
42
+ some_admin.admin? => true
43
+ some_admin.reader? => false
44
+
45
+ User.admin => [some_admin]
46
+ User.reader => []
47
+
48
+ No configuration is required. Custom STI column name is supported.
49
+
50
+ == License
51
+ +has_sti+ is released under the MIT license:
52
+ * http://www.opensource.org/licenses/MIT
53
+
54
+ == Author
55
+ Arkadiusz Fal
56
+ * http://arekf.net
57
+
58
+ Copyright © 2015
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'HasSti'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ begin
18
+ require 'rspec/core/rake_task'
19
+ RSpec::Core::RakeTask.new(:spec)
20
+ rescue LoadError
21
+ end
22
+
23
+ task default: [:spec]
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
@@ -0,0 +1,56 @@
1
+ module HasSti
2
+ module ActiveRecordExtensions
3
+ extend ActiveSupport::Concern
4
+
5
+ # Creates STI helper methods and scopes. Use it in your parent model, for example:
6
+ # class Cat < Animal; end
7
+ # class Dog < Animal; end
8
+ # class Parrot < Animal; end
9
+ #
10
+ # class Animal < ActiveRecord::Base
11
+ # has_sti :cat, :dog, :parrot
12
+ # end
13
+ # Helper methods will be created:
14
+ # cat = Cat.first
15
+ # cat.cat? => true
16
+ # cat.parrot? => false
17
+ # Also, you can use scopes on parent model, like:
18
+ # Animal.cat => array of Cats
19
+ def has_sti(*klasses)
20
+ raise HasSti::Exceptions::NoDescendantsError if klasses.count.zero?
21
+
22
+ superclass = constant(klasses.first).superclass
23
+ active_record_superclasses = [ActiveRecord::Base, Object, BasicObject]
24
+
25
+ raise HasSti::Exceptions::NoSuperclassError if superclass.nil? || active_record_superclasses.include?(superclass)
26
+
27
+ klasses.each do |klass|
28
+ define_method helper_method_name(klass) do
29
+ self.class.name == klass.to_s.classify
30
+ end
31
+
32
+ superclass.define_singleton_method scope_method_name(klass) do
33
+ where(superclass.inheritance_column => class_name(klass))
34
+ end
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def class_name(klass)
41
+ klass.to_s.classify
42
+ end
43
+
44
+ def constant(klass)
45
+ class_name(klass).constantize
46
+ end
47
+
48
+ def helper_method_name(klass)
49
+ "#{klass.to_s.underscore}?"
50
+ end
51
+
52
+ def scope_method_name(klass)
53
+ klass.to_s.underscore
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,14 @@
1
+ module HasSti
2
+ module Exceptions
3
+ class NoDescendantsError < StandardError
4
+ def message
5
+ 'You have not provided STI descendants for has_sti'
6
+ end
7
+ end
8
+ class NoSuperclassError < StandardError
9
+ def message
10
+ 'Your STI model has no parent model as a superclass'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module HasSti
2
+ VERSION = "0.0.1"
3
+ end
data/lib/has_sti.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'has_sti/active_record_extensions'
2
+ require 'has_sti/exceptions'
3
+
4
+ module HasSti
5
+ class ActiveRecord::Base
6
+ extend HasSti::ActiveRecordExtensions
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_sti
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Arkadiusz Fal
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 4.2.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 4.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
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
+ description: has_sti provides helper methods for Single Table Inheritance models of
70
+ Active Record. It creates methods that allow to determine exact type of model and
71
+ scopes that make easy to find records of certain type.
72
+ email:
73
+ - me@arekf.net
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - MIT-LICENSE
79
+ - README.rdoc
80
+ - Rakefile
81
+ - lib/has_sti.rb
82
+ - lib/has_sti/active_record_extensions.rb
83
+ - lib/has_sti/exceptions.rb
84
+ - lib/has_sti/version.rb
85
+ homepage: http://arekf.net
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Active Record extension that provides helper methods for Single Table Inheritance
109
+ models
110
+ test_files: []