acts_as_belongable 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +9 -0
- data/LICENSE +21 -0
- data/README.md +174 -0
- data/app/models/concerns/acts_as_belongable/belongable.rb +36 -0
- data/app/models/concerns/acts_as_belongable/belonger.rb +36 -0
- data/lib/acts_as_belongable.rb +8 -0
- data/lib/acts_as_belongable/engine.rb +6 -0
- data/lib/acts_as_belongable/railtie.rb +14 -0
- data/lib/acts_as_belongable/version.rb +5 -0
- data/lib/generators/acts_as_belongable_generator.rb +39 -0
- data/lib/generators/templates/README.md +1 -0
- data/lib/generators/templates/migration.rb.erb +14 -0
- data/lib/generators/templates/model.rb +8 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 99724979e1ee56f88f57d31b9fccae9fc557d6f5c03f3e09feecbbbb58587122
|
4
|
+
data.tar.gz: 8ea8c8e9c2b4de93e9a2db1ad9e5a56cde09c6b9e2fc953a236aaecd422607ad
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a135d7308b3fbaf37d83b639b9c2962180d6211a57ca904c22995e2b6264b1a4a0cf6028a6f8aedd5423e3446ea72eec78bfa73a54fa48b18d5e44e6ae10c781
|
7
|
+
data.tar.gz: 01f1e856c96e217f41d8c6973006d8c8e478158d5a830fb5d27cdf315fb598883d81279445888f177206bb0b07815f886c621d177224e58ee09625ba35c57b5a
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Jonas Hübotter
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
# acts_as_belongable
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/acts_as_belongable.svg)](https://badge.fury.io/rb/acts_as_belongable) <img src="https://travis-ci.org/jonhue/acts_as_belongable.svg?branch=master" />
|
4
|
+
|
5
|
+
acts_as_belongable is a Rubygem that provides an associations engine for Rails apps. It's primary use case is to simplify `has_many through: ...` relations.
|
6
|
+
|
7
|
+
---
|
8
|
+
|
9
|
+
## Table of Contents
|
10
|
+
|
11
|
+
* [Installation](#installation)
|
12
|
+
* [Usage](#usage)
|
13
|
+
* [Associations](#associations)
|
14
|
+
* [acts_as_belonger](#acts_as_belonger)
|
15
|
+
* [acts_as_belongable](#acts_as_belongable)
|
16
|
+
* [acts_as_list](#acts_as_list)
|
17
|
+
* [To Do](#to-do)
|
18
|
+
* [Contributing](#contributing)
|
19
|
+
* [Contributors](#contributors)
|
20
|
+
* [Semantic versioning](#semantic-versioning)
|
21
|
+
* [License](#license)
|
22
|
+
|
23
|
+
---
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
acts_as_belongable works with Rails 5 onwards. You can add it to your `Gemfile` with:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem 'acts_as_belongable'
|
31
|
+
```
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
|
35
|
+
$ bundle
|
36
|
+
|
37
|
+
Or install it yourself as:
|
38
|
+
|
39
|
+
$ gem install acts_as_belongable
|
40
|
+
|
41
|
+
If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
gem 'acts_as_belongable', github: 'jonhue/acts_as_belongable'
|
45
|
+
```
|
46
|
+
|
47
|
+
Now run the generator:
|
48
|
+
|
49
|
+
$ rails g acts_as_belongable
|
50
|
+
|
51
|
+
To wrap things up, migrate the changes into your database:
|
52
|
+
|
53
|
+
$ rails db:migrate
|
54
|
+
|
55
|
+
---
|
56
|
+
|
57
|
+
## Usage
|
58
|
+
|
59
|
+
### Associations
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class User < ApplicationRecord
|
63
|
+
acts_as_belongable
|
64
|
+
belongable :attending, 'Event'
|
65
|
+
end
|
66
|
+
|
67
|
+
class Event < ApplicationRecord
|
68
|
+
acts_as_belonger
|
69
|
+
acts_as_belongable
|
70
|
+
belonger :attendees, 'User'
|
71
|
+
belongable :conferences, 'Conference'
|
72
|
+
end
|
73
|
+
|
74
|
+
class Conference < ApplicationRecord
|
75
|
+
acts_as_belonger
|
76
|
+
belonger :events, 'Event'
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
### acts_as_belonger
|
81
|
+
|
82
|
+
`acts_as_belonger` makes the following methods available:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
c = Conference.first
|
86
|
+
|
87
|
+
# Returns all belongables associated with this record
|
88
|
+
c.belongables
|
89
|
+
|
90
|
+
# Adds a belongable to this record
|
91
|
+
c.add_belongable Event.first
|
92
|
+
# c.add_belongable! Event.first
|
93
|
+
|
94
|
+
# Creates a belongable record and adds it to this record
|
95
|
+
c.create_belongable 'Event', options
|
96
|
+
# c.create_belongable! Event, options
|
97
|
+
```
|
98
|
+
|
99
|
+
### acts_as_belongable
|
100
|
+
|
101
|
+
`acts_as_belongable` makes the following methods available:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
e = Event.first
|
105
|
+
|
106
|
+
# Returns all belongers associated with this record
|
107
|
+
e.belongers
|
108
|
+
|
109
|
+
# Adds this record to a belonger
|
110
|
+
c.add_to_belonger Conference.first
|
111
|
+
# c.add_to_belonger! Conference.first
|
112
|
+
|
113
|
+
# Adds this record to a newly created belonger
|
114
|
+
c.create_belonger 'Conference', options
|
115
|
+
# c.create_belonger! Conference, options
|
116
|
+
```
|
117
|
+
|
118
|
+
### acts_as_list
|
119
|
+
|
120
|
+
acts_as_belongable integrates with [acts_as_list](). It adds a `position` column to `Belonging`:
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
c.add_belongable Event.first, position: 1
|
124
|
+
```
|
125
|
+
|
126
|
+
---
|
127
|
+
|
128
|
+
## To Do
|
129
|
+
|
130
|
+
[Here](https://github.com/jonhue/acts_as_belongable/projects/1) is the full list of current projects.
|
131
|
+
|
132
|
+
To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/acts_as_belongable/issues/new).
|
133
|
+
|
134
|
+
---
|
135
|
+
|
136
|
+
## Contributing
|
137
|
+
|
138
|
+
We hope that you will consider contributing to acts_as_belongable. Please read this short overview for some information about how to get started:
|
139
|
+
|
140
|
+
[Learn more about contributing to this repository](CONTRIBUTING.md), [Code of Conduct](CODE_OF_CONDUCT.md)
|
141
|
+
|
142
|
+
### Contributors
|
143
|
+
|
144
|
+
Give the people some :heart: who are working on this project. See them all at:
|
145
|
+
|
146
|
+
https://github.com/jonhue/acts_as_belongable/graphs/contributors
|
147
|
+
|
148
|
+
### Semantic Versioning
|
149
|
+
|
150
|
+
acts_as_belongable follows Semantic Versioning 2.0 as defined at http://semver.org.
|
151
|
+
|
152
|
+
## License
|
153
|
+
|
154
|
+
MIT License
|
155
|
+
|
156
|
+
Copyright (c) 2018 Jonas Hübotter
|
157
|
+
|
158
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
159
|
+
of this software and associated documentation files (the "Software"), to deal
|
160
|
+
in the Software without restriction, including without limitation the rights
|
161
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
162
|
+
copies of the Software, and to permit persons to whom the Software is
|
163
|
+
furnished to do so, subject to the following conditions:
|
164
|
+
|
165
|
+
The above copyright notice and this permission notice shall be included in all
|
166
|
+
copies or substantial portions of the Software.
|
167
|
+
|
168
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
169
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
170
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
171
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
172
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
173
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
174
|
+
SOFTWARE.
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module ActsAsBelongable
|
2
|
+
module Belongable
|
3
|
+
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def acts_as_belongable
|
8
|
+
has_many :belongers, through: :belongable_belongings, source: :belonger
|
9
|
+
has_many :belongable_belongings, as: :belongable, class_name: 'Belonging', dependent: :destroy
|
10
|
+
end
|
11
|
+
|
12
|
+
def belongable name, source_type
|
13
|
+
has_many name.to_sym, through: :belongable_belongings, source: :belonger, source_type: source_type.to_s
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def add_to_belonger belonger, options = {}
|
18
|
+
options[:belonger] = belonger
|
19
|
+
self.belongable_belongings.create options
|
20
|
+
end
|
21
|
+
def add_to_belonger! belonger, options = {}
|
22
|
+
options[:belonger] = belonger
|
23
|
+
self.belongable_belongings.create! options
|
24
|
+
end
|
25
|
+
|
26
|
+
def create_belonger class_name, options = {}
|
27
|
+
object = class_name.constantize.create options
|
28
|
+
self.add_to_belonger object
|
29
|
+
end
|
30
|
+
def create_belonger! class_name, options = {}
|
31
|
+
object = class_name.constantize.create! options
|
32
|
+
self.add_to_belonger! object
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module ActsAsBelongable
|
2
|
+
module Belonger
|
3
|
+
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def acts_as_belonger
|
8
|
+
has_many :belongables, through: :belonger_belongings, source: :belongable
|
9
|
+
has_many :belonger_belongings, as: :belonger, class_name: 'Belonging', dependent: :destroy
|
10
|
+
end
|
11
|
+
|
12
|
+
def belonger name, source_type
|
13
|
+
has_many name.to_sym, through: :belonger_belongings, source: :belongable, source_type: source_type.to_s
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def add_belongable belongable, options = {}
|
18
|
+
options[:belongable] = belongable
|
19
|
+
self.belonger_belongings.create options
|
20
|
+
end
|
21
|
+
def add_belongable! belongable, options = {}
|
22
|
+
options[:belongable] = belongable
|
23
|
+
self.belonger_belongings.create! options
|
24
|
+
end
|
25
|
+
|
26
|
+
def create_belongable class_name, options = {}
|
27
|
+
object = class_name.constantize.create options
|
28
|
+
self.add_belongable object
|
29
|
+
end
|
30
|
+
def create_belongable! class_name, options = {}
|
31
|
+
object = class_name.constantize.create! options
|
32
|
+
self.add_belongable! object
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rails/railtie'
|
2
|
+
|
3
|
+
module ActsAsBelongable
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
|
6
|
+
initializer 'acts_as_belongable.active_record' do
|
7
|
+
ActiveSupport.on_load :active_record do
|
8
|
+
include ActsAsBelongable::Belonger
|
9
|
+
include ActsAsBelongable::Belongable
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
class ActsAsBelongableGenerator < Rails::Generators::Base
|
5
|
+
|
6
|
+
include Rails::Generators::Migration
|
7
|
+
|
8
|
+
source_root File.join File.dirname(__FILE__), 'templates'
|
9
|
+
desc 'Install acts_as_belongable'
|
10
|
+
|
11
|
+
def self.next_migration_number dirname
|
12
|
+
if ActiveRecord::Base.timestamped_migrations
|
13
|
+
Time.now.utc.strftime '%Y%m%d%H%M%S'
|
14
|
+
else
|
15
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_migration_file
|
20
|
+
migration_template 'migration.rb.erb', 'db/migrate/acts_as_belongable_migration.rb', migration_version: migration_version
|
21
|
+
end
|
22
|
+
|
23
|
+
def create_model
|
24
|
+
template 'model.rb', 'app/models/belonging.rb'
|
25
|
+
end
|
26
|
+
|
27
|
+
def show_readme
|
28
|
+
readme 'README.md'
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def migration_version
|
34
|
+
if Rails.version >= '5.0.0'
|
35
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Now run `rails db:migrate` to add acts_as_favoritor to your database.
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class ActsAsBelongableMigration < ActiveRecord::Migration<%= migration_version %>
|
2
|
+
def change
|
3
|
+
create_table :belongings do |t|
|
4
|
+
|
5
|
+
t.references :belonger, polymorphic: true, index: true
|
6
|
+
t.references :belonging, polymorphic: true, index: true
|
7
|
+
|
8
|
+
t.integer :position
|
9
|
+
|
10
|
+
t.timestamps null: false
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_belongable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonas Hübotter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-01-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activerecord
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: acts_as_list
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.9'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.9'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.7'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.7'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.52'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.52'
|
97
|
+
description: 'acts_as_belongable is a Rubygem that provides an associations engine
|
98
|
+
for Rails apps. It''s primary use case is to simplify `has_many through: ...` relations.'
|
99
|
+
email: me@jonhue.me
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- CHANGELOG.md
|
105
|
+
- LICENSE
|
106
|
+
- README.md
|
107
|
+
- app/models/concerns/acts_as_belongable/belongable.rb
|
108
|
+
- app/models/concerns/acts_as_belongable/belonger.rb
|
109
|
+
- lib/acts_as_belongable.rb
|
110
|
+
- lib/acts_as_belongable/engine.rb
|
111
|
+
- lib/acts_as_belongable/railtie.rb
|
112
|
+
- lib/acts_as_belongable/version.rb
|
113
|
+
- lib/generators/acts_as_belongable_generator.rb
|
114
|
+
- lib/generators/templates/README.md
|
115
|
+
- lib/generators/templates/migration.rb.erb
|
116
|
+
- lib/generators/templates/model.rb
|
117
|
+
homepage: https://github.com/jonhue/acts_as_belongable
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
metadata: {}
|
121
|
+
post_install_message: |
|
122
|
+
**Thank you for installing acts_as_belongable!**
|
123
|
+
Get started by running `rails g acts_as_belongable`.
|
124
|
+
Learn more at https://github.com/jonhue/acts_as_belongable.
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '2.3'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 2.7.4
|
141
|
+
signing_key:
|
142
|
+
specification_version: 4
|
143
|
+
summary: Associations engine for Rails
|
144
|
+
test_files: []
|