active_record-narcissus 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +0 -0
- data/LICENSE +21 -0
- data/README.md +65 -0
- data/lib/active_record/narcissus.rb +28 -0
- data/lib/active_record/narcissus/railtie.rb +16 -0
- data/lib/active_record/narcissus/reflection.rb +40 -0
- data/lib/active_record/narcissus/version.rb +5 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 68d1f9f070f434a258a9c5f98af66d6e692030be92c1a094d5975103cd326821
|
4
|
+
data.tar.gz: e78f39db6a8e843522ca6176fda10804f11ffeb3771d31412ba26077be3f71ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 66390b6ad49b4f3b6a62b68d8c22a5ba8399defd22caf414a7c70288aa73aa0a73c3edef69b3c086e1e1ac99583d4cf5fa96b1d4dbf089733e56f33430672428
|
7
|
+
data.tar.gz: 6fc74e643832ef151293420f4c8527a631363b79d9ab7802640978aeb107d9046d0b4b747898bd02f8a983edfd28ce8adb45b901448aeeb3c4f5ceb94ec69396
|
data/CHANGELOG.md
ADDED
File without changes
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Dale Stevens
|
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,65 @@
|
|
1
|
+
[![Version ](https://img.shields.io/gem/v/active_record-narcissus.svg)](https://rubygems.org/gems/active_record-narcissus)
|
2
|
+
[![Build Status ](https://travis-ci.org/TwilightCoders/active_record-narcissus.svg)](https://travis-ci.org/TwilightCoders/active_record-narcissus)
|
3
|
+
[![Code Quality](https://api.codeclimate.com/v1/badges/bed7d7fcb79cafb7384e/maintainability)](https://codeclimate.com/github/TwilightCoders/active_record-narcissus/maintainability)
|
4
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/bed7d7fcb79cafb7384e/test_coverage)](https://codeclimate.com/github/TwilightCoders/active_record-narcissus/test_coverage)
|
5
|
+
|
6
|
+
# ActiveRecord::Narcissus
|
7
|
+
|
8
|
+
`ActiveRecord::Narcissus` lends a helping hand to `ActiveRecord` when it comes to reflecting on the correct class for associations, particularly `:through` associations. See [usage](#usage) bellow for more details.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'active_record-narcissus'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install active_record-narcissus
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
ActiveRecord::Narcissus will automatically engage when ActiveRecord is loaded.
|
29
|
+
|
30
|
+
It works by slightly modifying the way ActiveRecord reflects on associations, particularly `:through` associations. Ths allows for the following to work automatically.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
|
34
|
+
Widget = Class.new(ActiveRecord::Base)
|
35
|
+
|
36
|
+
class User < ActiveRecord::Base
|
37
|
+
# These will be of type User::Widget by default based on namespacing
|
38
|
+
has_many :widgets
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
class User
|
43
|
+
class Widget < ::Widget
|
44
|
+
self.table_name = "user_widgets"
|
45
|
+
belongs_to :user
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class Post
|
50
|
+
belongs_to :user
|
51
|
+
has_many :user_widgets, through: :user, source: :widgets
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/TwilightCoders/active_record-narcissus). 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.
|
58
|
+
|
59
|
+
## License
|
60
|
+
|
61
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
62
|
+
|
63
|
+
## Code of Conduct
|
64
|
+
|
65
|
+
Everyone interacting in the ActiveRecord::Narcissus project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/TwilightCoders/active_record-narcissus/blob/master/CODE_OF_CONDUCT.md).
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "active_record/narcissus/version"
|
2
|
+
|
3
|
+
require 'active_support/all'
|
4
|
+
|
5
|
+
# require 'active_record/narcissus/config'
|
6
|
+
require 'active_record/narcissus/reflection'
|
7
|
+
require 'active_record/narcissus/railtie' if defined?(Rails::Railtie)
|
8
|
+
|
9
|
+
module ActiveRecord
|
10
|
+
module Narcissus
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_writer :logger
|
14
|
+
|
15
|
+
def logger
|
16
|
+
@logger ||= Logger.new($stdout).tap do |log|
|
17
|
+
log.progname = self.name
|
18
|
+
log.level = Logger::INFO
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.root
|
24
|
+
@root ||= Pathname.new(File.expand_path('../../', File.dirname(__FILE__)))
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rails/railtie'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Narcissus
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
initializer 'active_record-narcissus.load' do |_app|
|
7
|
+
ActiveRecord::Narcissus.logger.debug "active_record-narcissus.load"
|
8
|
+
ActiveSupport.on_load(:active_record) do
|
9
|
+
::ActiveRecord::Reflection::AssociationReflection.prepend Reflection
|
10
|
+
::ActiveRecord::Reflection::ThroughReflection.prepend ThroughReflection
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Narcissus
|
3
|
+
module Reflection
|
4
|
+
|
5
|
+
# def initialize(name, scope, options, active_record)
|
6
|
+
# super
|
7
|
+
# # options[:through] ? nil : klass
|
8
|
+
# end
|
9
|
+
|
10
|
+
def klass=(new_klass)
|
11
|
+
@klass = new_klass
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module ActiveRecord
|
19
|
+
module Narcissus
|
20
|
+
module ThroughReflection
|
21
|
+
|
22
|
+
def initialize(delegate_reflection)
|
23
|
+
super
|
24
|
+
delegate_reflection.klass = @klass
|
25
|
+
end
|
26
|
+
|
27
|
+
def through_klass
|
28
|
+
through = delegate_reflection.options[:through]
|
29
|
+
delegate_reflection.active_record.reflect_on_association(through).klass
|
30
|
+
end
|
31
|
+
|
32
|
+
def klass
|
33
|
+
@klass ||= through_klass.const_get(class_name)
|
34
|
+
rescue NameError
|
35
|
+
super
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record-narcissus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dale Stevens
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-28 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'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: pry-byebug
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '3'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '3'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.3'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.3'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rake
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '12.0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '12.0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: combustion
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0.7'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0.7'
|
89
|
+
description:
|
90
|
+
email:
|
91
|
+
- dale@twilightcoders.net
|
92
|
+
executables: []
|
93
|
+
extensions: []
|
94
|
+
extra_rdoc_files: []
|
95
|
+
files:
|
96
|
+
- CHANGELOG.md
|
97
|
+
- LICENSE
|
98
|
+
- README.md
|
99
|
+
- lib/active_record/narcissus.rb
|
100
|
+
- lib/active_record/narcissus/railtie.rb
|
101
|
+
- lib/active_record/narcissus/reflection.rb
|
102
|
+
- lib/active_record/narcissus/version.rb
|
103
|
+
homepage: https://github.com/TwilightCoders/active_record-narcissus
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata:
|
107
|
+
allowed_push_host: https://rubygems.org
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '2.3'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubygems_version: 3.0.1
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: Improves the class reflection of relationships
|
127
|
+
test_files: []
|