auto_scopes 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: 290163ddcf452c87a4136de4cf3e7f907f124b95
4
+ data.tar.gz: a3620f13ff4a91a600923fa6296178c4d707945d
5
+ SHA512:
6
+ metadata.gz: f3a18e83cefd419f812b3ad922bebff1a962ae66d6ef4102d86ef4d26191f9e17f9fa610ee77e85b6e24de4f02f6bf7daa02658bb2cfbaec4f4a603df223aa3d
7
+ data.tar.gz: 93815581932eb7394b9eedb2fc2eef851f77c21b494eab608c26c8474e4594e3ea846874b04a9465e9231c848abeabee5988cb148d7b79207141324d3e0977ec
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.rvmrc
11
+ /spec/dummy/log/
12
+ /spec/dummy/db/test.sqlite3
13
+ spec/tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2
4
+ - 2.1
5
+ - 2.0
6
+ before_script:
7
+ - gem install bundler
8
+ - "cd spec/dummy"
9
+ - "RAILS_ENV=test bundle exec rake db:migrate --trace"
10
+ - "cd ../.."
11
+ script: "bundle exec rspec"
12
+ gemfile:
13
+ - gemfiles/3.2.gemfile
14
+ - gemfiles/4.0.gemfile
15
+ - gemfiles/4.1.gemfile
16
+ - gemfiles/4.2.gemfile
data/Appraisals ADDED
@@ -0,0 +1,6 @@
1
+ %w(3.2 4.0 4.1 4.2).each do |rails_version|
2
+ appraise rails_version do
3
+ gem 'test-unit', '~> 3.0' if rails_version == '3.2'
4
+ gem "rails", "~> #{rails_version}"
5
+ end
6
+ end
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in auto_scopes.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Victor Palomo de Castro
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.
22
+
data/README.md ADDED
@@ -0,0 +1,123 @@
1
+ # AutoScopes
2
+ [![Build Status](https://travis-ci.org/victor95pc/auto-scopes.svg?branch=master)](https://travis-ci.org/victor95pc/auto-scopes)
3
+
4
+ Recycle already defined scopes on multiple models whatever the association are, making your models way more DRYer
5
+
6
+ I always wonder if it is possible to reuse some scopes defined in a deep associations, we all know that is possible using merges but it still not a clean solution, I still need to define new scopes and make joins so Rails can know which model I want, so I started to develop this gem, the idea is easy to follow, put all scopes you want inside a YAML file.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'auto_scopes'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+
21
+ ## Requirements
22
+
23
+ - Rails 3.2.x or Rails 4.x
24
+
25
+ - Ruby >= 2.0.0
26
+
27
+
28
+ ## Usage
29
+
30
+ After you install AutoScopes and add it to your Gemfile, you need to run the generator:
31
+
32
+ ```console
33
+ rake auto_scopes:install
34
+ ```
35
+
36
+ The generator will install an initializer with some instructions and a YAML
37
+
38
+
39
+ Once you setup everything, time to learn how recycle your scopes, first you must open the generated file in config/autoscopes.yml, I will try to explain using a example:
40
+
41
+ **Models:**
42
+ ```ruby
43
+ class Ceo < ActiveRecord::Base
44
+ has_many :companies
45
+ end
46
+ end
47
+ ```
48
+
49
+ ```ruby
50
+ class Company < ActiveRecord::Base
51
+ belongs_to :ceo
52
+ has_many :employeds
53
+ end
54
+ ```
55
+ ```ruby
56
+ class Employed < ActiveRecord::Base
57
+ belongs_to :company
58
+
59
+ def self.by_name(name)
60
+ where(name: name)
61
+ end
62
+
63
+ def self.age(age)
64
+ where(age: name)
65
+ end
66
+ end
67
+ ```
68
+ If you want to get a CEOs by a employed´s name or age, you must write your auto_scopes file like this one:
69
+ ```yaml
70
+ # config/auto_scopes.yml
71
+ Ceo:
72
+ companies:
73
+ employeds:
74
+ - by_name
75
+ - age
76
+ ```
77
+
78
+ Doesn't matter the deepness of your association, auto_scopes will make joins to there, and call merge, it will produce 2 methods inside CEO´s model:
79
+
80
+ ```ruby
81
+ # app/models/ceo.rb
82
+ def self.by_name(name)
83
+ joins(companies: :employeds).merge(Employed.by_name(name))
84
+ end
85
+
86
+ def self.age(age)
87
+ joins(companies: :employeds).merge(Employed.age(age))
88
+ end
89
+ ```
90
+
91
+ Auto_scopes also will create scope for each association, for example
92
+ ```ruby
93
+ # app/models/ceo.rb
94
+ def self.by_companies(id)
95
+ joins(companies).where('companies.id = ?', id)
96
+ end
97
+
98
+ def self.by_employed(id)
99
+ joins(companies: :employeds).where('employeds.id = ?', id)
100
+ end
101
+ ```
102
+
103
+ If you dont want this behavior you can set the option create_scope_for_association to false
104
+ ```ruby
105
+ # config/initializers/auto_scopes.rb
106
+ AutoScopes.configure do |c|
107
+ c.create_scope_for_association = false
108
+ end
109
+ ```
110
+
111
+ ## Contributing
112
+
113
+ 1. Fork it ( https://github.com/victor95pc/auto-scopes/fork )
114
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
115
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
116
+ 4. Push to the branch (`git push origin my-new-feature`)
117
+ 5. Create a new Pull Request
118
+
119
+ Report issues or feature requests to [GitHub Issues](https://github.com/victor95pc/auto-scopes/issues).
120
+
121
+ ## License
122
+
123
+ <tt>AutoScopes</tt> is Copyright (c) 2015 Victor Palomo de Castro. This is Free Software distributed under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "rubygems"
4
+ require "bundler/setup"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'auto_scopes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "auto_scopes"
8
+ spec.version = AutoScopes::VERSION
9
+ spec.authors = ["victor95pc"]
10
+ spec.email = ["victorpalomocastro@gmail.com"]
11
+
12
+ spec.summary = %q{Recycle already defined scopes on multiple models whatever the association are, making your models way more DRYer}
13
+ spec.description = %q{I always wonder if it was possible to reuse some scopes defined in a deep associations, we all know that is possible using merges but it still not a clean solution, I still need to define new scopes and make joins so Rails can know which model I want, so I started to develop this gem, the idea is easy to follow, put all scopes you want inside a YAML file.}
14
+ spec.homepage = "https://github.com/victor95pc/auto-scopes"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = ">= 2.0.0"
22
+ spec.add_dependency "rails", ">= 3.2.0"
23
+ spec.add_dependency "configurations", '~> 2.2.0'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.8"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "appraisal"
29
+ spec.add_development_dependency "pry"
30
+ spec.add_development_dependency "sqlite3"
31
+ spec.add_development_dependency "generator_spec"
32
+ end
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pry"
5
+ require_relative '../spec/spec_helper'
6
+
7
+ require "irb"
8
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1 @@
1
+ # how to make associations: https://github.com/victor95pc/auto-scopes
@@ -0,0 +1,12 @@
1
+ # auto_scopes_location: where autoscopes must be defined
2
+ #
3
+ # create_scope_for_association: in each association the parent model
4
+ # a scope with id is create this option set this behavior
5
+ #
6
+ # scope_association_prefix: in each association the parent model
7
+ # this is the prefix added to them
8
+ AutoScopes.configure do |c|
9
+ # c.auto_scopes_location = 'config/auto_scopes'
10
+ # c.create_scope_for_association = true
11
+ # c.scope_association_prefix = 'by'
12
+ end
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "test-unit", "~> 3.0"
6
+ gem "rails", "~> 3.2"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1,134 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ auto_scopes (0.1.0)
5
+ configurations (~> 2.2.0)
6
+ rails (>= 3.2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionmailer (3.2.22)
12
+ actionpack (= 3.2.22)
13
+ mail (~> 2.5.4)
14
+ actionpack (3.2.22)
15
+ activemodel (= 3.2.22)
16
+ activesupport (= 3.2.22)
17
+ builder (~> 3.0.0)
18
+ erubis (~> 2.7.0)
19
+ journey (~> 1.0.4)
20
+ rack (~> 1.4.5)
21
+ rack-cache (~> 1.2)
22
+ rack-test (~> 0.6.1)
23
+ sprockets (~> 2.2.1)
24
+ activemodel (3.2.22)
25
+ activesupport (= 3.2.22)
26
+ builder (~> 3.0.0)
27
+ activerecord (3.2.22)
28
+ activemodel (= 3.2.22)
29
+ activesupport (= 3.2.22)
30
+ arel (~> 3.0.2)
31
+ tzinfo (~> 0.3.29)
32
+ activeresource (3.2.22)
33
+ activemodel (= 3.2.22)
34
+ activesupport (= 3.2.22)
35
+ activesupport (3.2.22)
36
+ i18n (~> 0.6, >= 0.6.4)
37
+ multi_json (~> 1.0)
38
+ appraisal (2.1.0)
39
+ bundler
40
+ rake
41
+ thor (>= 0.14.0)
42
+ arel (3.0.3)
43
+ builder (3.0.4)
44
+ coderay (1.1.0)
45
+ configurations (2.2.0)
46
+ diff-lcs (1.2.5)
47
+ erubis (2.7.0)
48
+ generator_spec (0.9.3)
49
+ activesupport (>= 3.0.0)
50
+ railties (>= 3.0.0)
51
+ hike (1.2.3)
52
+ i18n (0.7.0)
53
+ journey (1.0.4)
54
+ json (1.8.3)
55
+ mail (2.5.4)
56
+ mime-types (~> 1.16)
57
+ treetop (~> 1.4.8)
58
+ method_source (0.8.2)
59
+ mime-types (1.25.1)
60
+ multi_json (1.11.2)
61
+ polyglot (0.3.5)
62
+ power_assert (0.2.2)
63
+ pry (0.10.3)
64
+ coderay (~> 1.1.0)
65
+ method_source (~> 0.8.1)
66
+ slop (~> 3.4)
67
+ rack (1.4.7)
68
+ rack-cache (1.5.0)
69
+ rack (>= 0.4)
70
+ rack-ssl (1.3.4)
71
+ rack
72
+ rack-test (0.6.3)
73
+ rack (>= 1.0)
74
+ rails (3.2.22)
75
+ actionmailer (= 3.2.22)
76
+ actionpack (= 3.2.22)
77
+ activerecord (= 3.2.22)
78
+ activeresource (= 3.2.22)
79
+ activesupport (= 3.2.22)
80
+ bundler (~> 1.0)
81
+ railties (= 3.2.22)
82
+ railties (3.2.22)
83
+ actionpack (= 3.2.22)
84
+ activesupport (= 3.2.22)
85
+ rack-ssl (~> 1.3.2)
86
+ rake (>= 0.8.7)
87
+ rdoc (~> 3.4)
88
+ thor (>= 0.14.6, < 2.0)
89
+ rake (10.4.2)
90
+ rdoc (3.12.2)
91
+ json (~> 1.4)
92
+ rspec (3.3.0)
93
+ rspec-core (~> 3.3.0)
94
+ rspec-expectations (~> 3.3.0)
95
+ rspec-mocks (~> 3.3.0)
96
+ rspec-core (3.3.2)
97
+ rspec-support (~> 3.3.0)
98
+ rspec-expectations (3.3.1)
99
+ diff-lcs (>= 1.2.0, < 2.0)
100
+ rspec-support (~> 3.3.0)
101
+ rspec-mocks (3.3.2)
102
+ diff-lcs (>= 1.2.0, < 2.0)
103
+ rspec-support (~> 3.3.0)
104
+ rspec-support (3.3.0)
105
+ slop (3.6.0)
106
+ sprockets (2.2.3)
107
+ hike (~> 1.2)
108
+ multi_json (~> 1.0)
109
+ rack (~> 1.0)
110
+ tilt (~> 1.1, != 1.3.0)
111
+ sqlite3 (1.3.11)
112
+ test-unit (3.0.8)
113
+ power_assert
114
+ thor (0.19.1)
115
+ tilt (1.4.1)
116
+ treetop (1.4.15)
117
+ polyglot
118
+ polyglot (>= 0.3.1)
119
+ tzinfo (0.3.45)
120
+
121
+ PLATFORMS
122
+ ruby
123
+
124
+ DEPENDENCIES
125
+ appraisal
126
+ auto_scopes!
127
+ bundler (~> 1.8)
128
+ generator_spec
129
+ pry
130
+ rails (~> 3.2)
131
+ rake (~> 10.0)
132
+ rspec (~> 3.0)
133
+ sqlite3
134
+ test-unit (~> 3.0)
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 4.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,143 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ auto_scopes (0.1.0)
5
+ configurations (~> 2.2.0)
6
+ rails (>= 3.2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionmailer (4.2.4)
12
+ actionpack (= 4.2.4)
13
+ actionview (= 4.2.4)
14
+ activejob (= 4.2.4)
15
+ mail (~> 2.5, >= 2.5.4)
16
+ rails-dom-testing (~> 1.0, >= 1.0.5)
17
+ actionpack (4.2.4)
18
+ actionview (= 4.2.4)
19
+ activesupport (= 4.2.4)
20
+ rack (~> 1.6)
21
+ rack-test (~> 0.6.2)
22
+ rails-dom-testing (~> 1.0, >= 1.0.5)
23
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
24
+ actionview (4.2.4)
25
+ activesupport (= 4.2.4)
26
+ builder (~> 3.1)
27
+ erubis (~> 2.7.0)
28
+ rails-dom-testing (~> 1.0, >= 1.0.5)
29
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
30
+ activejob (4.2.4)
31
+ activesupport (= 4.2.4)
32
+ globalid (>= 0.3.0)
33
+ activemodel (4.2.4)
34
+ activesupport (= 4.2.4)
35
+ builder (~> 3.1)
36
+ activerecord (4.2.4)
37
+ activemodel (= 4.2.4)
38
+ activesupport (= 4.2.4)
39
+ arel (~> 6.0)
40
+ activesupport (4.2.4)
41
+ i18n (~> 0.7)
42
+ json (~> 1.7, >= 1.7.7)
43
+ minitest (~> 5.1)
44
+ thread_safe (~> 0.3, >= 0.3.4)
45
+ tzinfo (~> 1.1)
46
+ appraisal (2.1.0)
47
+ bundler
48
+ rake
49
+ thor (>= 0.14.0)
50
+ arel (6.0.3)
51
+ builder (3.2.2)
52
+ coderay (1.1.0)
53
+ configurations (2.2.0)
54
+ diff-lcs (1.2.5)
55
+ erubis (2.7.0)
56
+ generator_spec (0.9.3)
57
+ activesupport (>= 3.0.0)
58
+ railties (>= 3.0.0)
59
+ globalid (0.3.6)
60
+ activesupport (>= 4.1.0)
61
+ i18n (0.7.0)
62
+ json (1.8.3)
63
+ loofah (2.0.3)
64
+ nokogiri (>= 1.5.9)
65
+ mail (2.6.3)
66
+ mime-types (>= 1.16, < 3)
67
+ method_source (0.8.2)
68
+ mime-types (2.6.2)
69
+ mini_portile (0.6.2)
70
+ minitest (5.8.1)
71
+ nokogiri (1.6.6.2)
72
+ mini_portile (~> 0.6.0)
73
+ pry (0.10.3)
74
+ coderay (~> 1.1.0)
75
+ method_source (~> 0.8.1)
76
+ slop (~> 3.4)
77
+ rack (1.6.4)
78
+ rack-test (0.6.3)
79
+ rack (>= 1.0)
80
+ rails (4.2.4)
81
+ actionmailer (= 4.2.4)
82
+ actionpack (= 4.2.4)
83
+ actionview (= 4.2.4)
84
+ activejob (= 4.2.4)
85
+ activemodel (= 4.2.4)
86
+ activerecord (= 4.2.4)
87
+ activesupport (= 4.2.4)
88
+ bundler (>= 1.3.0, < 2.0)
89
+ railties (= 4.2.4)
90
+ sprockets-rails
91
+ rails-deprecated_sanitizer (1.0.3)
92
+ activesupport (>= 4.2.0.alpha)
93
+ rails-dom-testing (1.0.7)
94
+ activesupport (>= 4.2.0.beta, < 5.0)
95
+ nokogiri (~> 1.6.0)
96
+ rails-deprecated_sanitizer (>= 1.0.1)
97
+ rails-html-sanitizer (1.0.2)
98
+ loofah (~> 2.0)
99
+ railties (4.2.4)
100
+ actionpack (= 4.2.4)
101
+ activesupport (= 4.2.4)
102
+ rake (>= 0.8.7)
103
+ thor (>= 0.18.1, < 2.0)
104
+ rake (10.4.2)
105
+ rspec (3.3.0)
106
+ rspec-core (~> 3.3.0)
107
+ rspec-expectations (~> 3.3.0)
108
+ rspec-mocks (~> 3.3.0)
109
+ rspec-core (3.3.2)
110
+ rspec-support (~> 3.3.0)
111
+ rspec-expectations (3.3.1)
112
+ diff-lcs (>= 1.2.0, < 2.0)
113
+ rspec-support (~> 3.3.0)
114
+ rspec-mocks (3.3.2)
115
+ diff-lcs (>= 1.2.0, < 2.0)
116
+ rspec-support (~> 3.3.0)
117
+ rspec-support (3.3.0)
118
+ slop (3.6.0)
119
+ sprockets (3.4.0)
120
+ rack (> 1, < 3)
121
+ sprockets-rails (2.3.3)
122
+ actionpack (>= 3.0)
123
+ activesupport (>= 3.0)
124
+ sprockets (>= 2.8, < 4.0)
125
+ sqlite3 (1.3.11)
126
+ thor (0.19.1)
127
+ thread_safe (0.3.5)
128
+ tzinfo (1.2.2)
129
+ thread_safe (~> 0.1)
130
+
131
+ PLATFORMS
132
+ ruby
133
+
134
+ DEPENDENCIES
135
+ appraisal
136
+ auto_scopes!
137
+ bundler (~> 1.8)
138
+ generator_spec
139
+ pry
140
+ rails (~> 4.0)
141
+ rake (~> 10.0)
142
+ rspec (~> 3.0)
143
+ sqlite3
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 4.1"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,143 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ auto_scopes (0.1.0)
5
+ configurations (~> 2.2.0)
6
+ rails (>= 3.2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionmailer (4.2.4)
12
+ actionpack (= 4.2.4)
13
+ actionview (= 4.2.4)
14
+ activejob (= 4.2.4)
15
+ mail (~> 2.5, >= 2.5.4)
16
+ rails-dom-testing (~> 1.0, >= 1.0.5)
17
+ actionpack (4.2.4)
18
+ actionview (= 4.2.4)
19
+ activesupport (= 4.2.4)
20
+ rack (~> 1.6)
21
+ rack-test (~> 0.6.2)
22
+ rails-dom-testing (~> 1.0, >= 1.0.5)
23
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
24
+ actionview (4.2.4)
25
+ activesupport (= 4.2.4)
26
+ builder (~> 3.1)
27
+ erubis (~> 2.7.0)
28
+ rails-dom-testing (~> 1.0, >= 1.0.5)
29
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
30
+ activejob (4.2.4)
31
+ activesupport (= 4.2.4)
32
+ globalid (>= 0.3.0)
33
+ activemodel (4.2.4)
34
+ activesupport (= 4.2.4)
35
+ builder (~> 3.1)
36
+ activerecord (4.2.4)
37
+ activemodel (= 4.2.4)
38
+ activesupport (= 4.2.4)
39
+ arel (~> 6.0)
40
+ activesupport (4.2.4)
41
+ i18n (~> 0.7)
42
+ json (~> 1.7, >= 1.7.7)
43
+ minitest (~> 5.1)
44
+ thread_safe (~> 0.3, >= 0.3.4)
45
+ tzinfo (~> 1.1)
46
+ appraisal (2.1.0)
47
+ bundler
48
+ rake
49
+ thor (>= 0.14.0)
50
+ arel (6.0.3)
51
+ builder (3.2.2)
52
+ coderay (1.1.0)
53
+ configurations (2.2.0)
54
+ diff-lcs (1.2.5)
55
+ erubis (2.7.0)
56
+ generator_spec (0.9.3)
57
+ activesupport (>= 3.0.0)
58
+ railties (>= 3.0.0)
59
+ globalid (0.3.6)
60
+ activesupport (>= 4.1.0)
61
+ i18n (0.7.0)
62
+ json (1.8.3)
63
+ loofah (2.0.3)
64
+ nokogiri (>= 1.5.9)
65
+ mail (2.6.3)
66
+ mime-types (>= 1.16, < 3)
67
+ method_source (0.8.2)
68
+ mime-types (2.6.2)
69
+ mini_portile (0.6.2)
70
+ minitest (5.8.1)
71
+ nokogiri (1.6.6.2)
72
+ mini_portile (~> 0.6.0)
73
+ pry (0.10.3)
74
+ coderay (~> 1.1.0)
75
+ method_source (~> 0.8.1)
76
+ slop (~> 3.4)
77
+ rack (1.6.4)
78
+ rack-test (0.6.3)
79
+ rack (>= 1.0)
80
+ rails (4.2.4)
81
+ actionmailer (= 4.2.4)
82
+ actionpack (= 4.2.4)
83
+ actionview (= 4.2.4)
84
+ activejob (= 4.2.4)
85
+ activemodel (= 4.2.4)
86
+ activerecord (= 4.2.4)
87
+ activesupport (= 4.2.4)
88
+ bundler (>= 1.3.0, < 2.0)
89
+ railties (= 4.2.4)
90
+ sprockets-rails
91
+ rails-deprecated_sanitizer (1.0.3)
92
+ activesupport (>= 4.2.0.alpha)
93
+ rails-dom-testing (1.0.7)
94
+ activesupport (>= 4.2.0.beta, < 5.0)
95
+ nokogiri (~> 1.6.0)
96
+ rails-deprecated_sanitizer (>= 1.0.1)
97
+ rails-html-sanitizer (1.0.2)
98
+ loofah (~> 2.0)
99
+ railties (4.2.4)
100
+ actionpack (= 4.2.4)
101
+ activesupport (= 4.2.4)
102
+ rake (>= 0.8.7)
103
+ thor (>= 0.18.1, < 2.0)
104
+ rake (10.4.2)
105
+ rspec (3.3.0)
106
+ rspec-core (~> 3.3.0)
107
+ rspec-expectations (~> 3.3.0)
108
+ rspec-mocks (~> 3.3.0)
109
+ rspec-core (3.3.2)
110
+ rspec-support (~> 3.3.0)
111
+ rspec-expectations (3.3.1)
112
+ diff-lcs (>= 1.2.0, < 2.0)
113
+ rspec-support (~> 3.3.0)
114
+ rspec-mocks (3.3.2)
115
+ diff-lcs (>= 1.2.0, < 2.0)
116
+ rspec-support (~> 3.3.0)
117
+ rspec-support (3.3.0)
118
+ slop (3.6.0)
119
+ sprockets (3.4.0)
120
+ rack (> 1, < 3)
121
+ sprockets-rails (2.3.3)
122
+ actionpack (>= 3.0)
123
+ activesupport (>= 3.0)
124
+ sprockets (>= 2.8, < 4.0)
125
+ sqlite3 (1.3.11)
126
+ thor (0.19.1)
127
+ thread_safe (0.3.5)
128
+ tzinfo (1.2.2)
129
+ thread_safe (~> 0.1)
130
+
131
+ PLATFORMS
132
+ ruby
133
+
134
+ DEPENDENCIES
135
+ appraisal
136
+ auto_scopes!
137
+ bundler (~> 1.8)
138
+ generator_spec
139
+ pry
140
+ rails (~> 4.1)
141
+ rake (~> 10.0)
142
+ rspec (~> 3.0)
143
+ sqlite3
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 4.2"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,143 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ auto_scopes (0.1.0)
5
+ configurations (~> 2.2.0)
6
+ rails (>= 3.2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionmailer (4.2.4)
12
+ actionpack (= 4.2.4)
13
+ actionview (= 4.2.4)
14
+ activejob (= 4.2.4)
15
+ mail (~> 2.5, >= 2.5.4)
16
+ rails-dom-testing (~> 1.0, >= 1.0.5)
17
+ actionpack (4.2.4)
18
+ actionview (= 4.2.4)
19
+ activesupport (= 4.2.4)
20
+ rack (~> 1.6)
21
+ rack-test (~> 0.6.2)
22
+ rails-dom-testing (~> 1.0, >= 1.0.5)
23
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
24
+ actionview (4.2.4)
25
+ activesupport (= 4.2.4)
26
+ builder (~> 3.1)
27
+ erubis (~> 2.7.0)
28
+ rails-dom-testing (~> 1.0, >= 1.0.5)
29
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
30
+ activejob (4.2.4)
31
+ activesupport (= 4.2.4)
32
+ globalid (>= 0.3.0)
33
+ activemodel (4.2.4)
34
+ activesupport (= 4.2.4)
35
+ builder (~> 3.1)
36
+ activerecord (4.2.4)
37
+ activemodel (= 4.2.4)
38
+ activesupport (= 4.2.4)
39
+ arel (~> 6.0)
40
+ activesupport (4.2.4)
41
+ i18n (~> 0.7)
42
+ json (~> 1.7, >= 1.7.7)
43
+ minitest (~> 5.1)
44
+ thread_safe (~> 0.3, >= 0.3.4)
45
+ tzinfo (~> 1.1)
46
+ appraisal (2.1.0)
47
+ bundler
48
+ rake
49
+ thor (>= 0.14.0)
50
+ arel (6.0.3)
51
+ builder (3.2.2)
52
+ coderay (1.1.0)
53
+ configurations (2.2.0)
54
+ diff-lcs (1.2.5)
55
+ erubis (2.7.0)
56
+ generator_spec (0.9.3)
57
+ activesupport (>= 3.0.0)
58
+ railties (>= 3.0.0)
59
+ globalid (0.3.6)
60
+ activesupport (>= 4.1.0)
61
+ i18n (0.7.0)
62
+ json (1.8.3)
63
+ loofah (2.0.3)
64
+ nokogiri (>= 1.5.9)
65
+ mail (2.6.3)
66
+ mime-types (>= 1.16, < 3)
67
+ method_source (0.8.2)
68
+ mime-types (2.6.2)
69
+ mini_portile (0.6.2)
70
+ minitest (5.8.1)
71
+ nokogiri (1.6.6.2)
72
+ mini_portile (~> 0.6.0)
73
+ pry (0.10.3)
74
+ coderay (~> 1.1.0)
75
+ method_source (~> 0.8.1)
76
+ slop (~> 3.4)
77
+ rack (1.6.4)
78
+ rack-test (0.6.3)
79
+ rack (>= 1.0)
80
+ rails (4.2.4)
81
+ actionmailer (= 4.2.4)
82
+ actionpack (= 4.2.4)
83
+ actionview (= 4.2.4)
84
+ activejob (= 4.2.4)
85
+ activemodel (= 4.2.4)
86
+ activerecord (= 4.2.4)
87
+ activesupport (= 4.2.4)
88
+ bundler (>= 1.3.0, < 2.0)
89
+ railties (= 4.2.4)
90
+ sprockets-rails
91
+ rails-deprecated_sanitizer (1.0.3)
92
+ activesupport (>= 4.2.0.alpha)
93
+ rails-dom-testing (1.0.7)
94
+ activesupport (>= 4.2.0.beta, < 5.0)
95
+ nokogiri (~> 1.6.0)
96
+ rails-deprecated_sanitizer (>= 1.0.1)
97
+ rails-html-sanitizer (1.0.2)
98
+ loofah (~> 2.0)
99
+ railties (4.2.4)
100
+ actionpack (= 4.2.4)
101
+ activesupport (= 4.2.4)
102
+ rake (>= 0.8.7)
103
+ thor (>= 0.18.1, < 2.0)
104
+ rake (10.4.2)
105
+ rspec (3.3.0)
106
+ rspec-core (~> 3.3.0)
107
+ rspec-expectations (~> 3.3.0)
108
+ rspec-mocks (~> 3.3.0)
109
+ rspec-core (3.3.2)
110
+ rspec-support (~> 3.3.0)
111
+ rspec-expectations (3.3.1)
112
+ diff-lcs (>= 1.2.0, < 2.0)
113
+ rspec-support (~> 3.3.0)
114
+ rspec-mocks (3.3.2)
115
+ diff-lcs (>= 1.2.0, < 2.0)
116
+ rspec-support (~> 3.3.0)
117
+ rspec-support (3.3.0)
118
+ slop (3.6.0)
119
+ sprockets (3.4.0)
120
+ rack (> 1, < 3)
121
+ sprockets-rails (2.3.3)
122
+ actionpack (>= 3.0)
123
+ activesupport (>= 3.0)
124
+ sprockets (>= 2.8, < 4.0)
125
+ sqlite3 (1.3.11)
126
+ thor (0.19.1)
127
+ thread_safe (0.3.5)
128
+ tzinfo (1.2.2)
129
+ thread_safe (~> 0.1)
130
+
131
+ PLATFORMS
132
+ ruby
133
+
134
+ DEPENDENCIES
135
+ appraisal
136
+ auto_scopes!
137
+ bundler (~> 1.8)
138
+ generator_spec
139
+ pry
140
+ rails (~> 4.2)
141
+ rake (~> 10.0)
142
+ rspec (~> 3.0)
143
+ sqlite3
@@ -0,0 +1,46 @@
1
+ module AutoScopes
2
+ class AssociationsChains < Struct.new(:klass, :associations, :path)
3
+
4
+ delegate :config, to: AutoScopes
5
+
6
+ attr_accessor :association
7
+
8
+ def look_inside_chain
9
+ associations.each_pair do |association_parent, association|
10
+ self.association = association
11
+
12
+ path.push(association_parent)
13
+
14
+ method_maker.create_association if config.create_scope_for_association
15
+
16
+ if nested_hash?
17
+ self.class.new(klass, association, path.clone).look_inside_chain
18
+ else
19
+ call_methods_maker
20
+ end
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def method_maker(another_path = self.path)
27
+ AssociationsMethods.new(self, another_path)
28
+ end
29
+
30
+ def call_methods_maker
31
+ if methods_inside?
32
+ method_maker.create_merge(association)
33
+ elsif association.present?
34
+ method_maker(path + [associations]).create_association
35
+ end
36
+ end
37
+
38
+ def nested_hash?
39
+ association.is_a? Hash
40
+ end
41
+
42
+ def methods_inside?
43
+ association.is_a? Array
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,65 @@
1
+ module AutoScopes
2
+ class AssociationsMethods < Struct.new(:associations_chains)
3
+ attr_accessor :path
4
+
5
+ delegate :klass, to: :associations_chains
6
+
7
+ delegate :config, to: AutoScopes
8
+
9
+ def initialize(associations_chains, path = associations_chains.path)
10
+ self.associations_chains = associations_chains
11
+ self.path = path.clone
12
+ end
13
+
14
+ def create_merge(methods)
15
+ association_klass = get_last_association_from_path
16
+ joins_path = organize_joins
17
+
18
+ methods.each do |method|
19
+ klass.define_singleton_method(method) do |*args|
20
+ joins(joins_path).merge association_klass.send(method, *args)
21
+ end
22
+ end
23
+ end
24
+
25
+ def create_association
26
+ association_klass, field = get_last_association_from_path do |klass, foreign_key|
27
+ path.pop
28
+ [klass, foreign_key]
29
+ end
30
+
31
+ joins_path = organize_joins
32
+
33
+ field ||= 'id'
34
+
35
+ klass.define_singleton_method("#{config.scope_association_prefix}_#{associations_chains.path.last}") do |id|
36
+ joins(joins_path).where(association_klass.table_name => { field => id })
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def organize_joins
43
+ path.map(&:to_sym).reverse.inject() { |a, n| { n => a } }
44
+ end
45
+
46
+ def get_last_association_from_path
47
+ path.inject(klass) do |klass, reflection|
48
+
49
+ reflection_access = if Rails.version < '4.2'
50
+ reflection.to_sym
51
+ else
52
+ reflection
53
+ end
54
+
55
+ reflection_klass = klass.reflections[reflection_access]
56
+
57
+ if block_given? && path.last == reflection && reflection_klass.macro == :belongs_to
58
+ yield klass, reflection_klass.foreign_key
59
+ else
60
+ reflection_klass.class_name.constantize
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,10 @@
1
+ module AutoScopes
2
+ class CheckModels < Rails::Railtie
3
+ config.after_initialize do
4
+ AutoScopes.associations_source.each_pair do |model, associations|
5
+ AssociationsChains.new(model.constantize, associations, []).look_inside_chain
6
+ end
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,3 @@
1
+ module AutoScopes
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,34 @@
1
+ require 'yaml'
2
+ require 'configurations'
3
+ require "rails/paths"
4
+ require "active_support/concern"
5
+ require 'active_support/core_ext/module/delegation'
6
+ require 'rails/railtie'
7
+ require 'active_record'
8
+
9
+ require "auto_scopes/version"
10
+ require "auto_scopes/associations_methods"
11
+ require "auto_scopes/associations_chains"
12
+ require "auto_scopes/check_models"
13
+
14
+ module AutoScopes
15
+ include Configurations
16
+
17
+ configurable :auto_scopes_location, :scope_association_prefix, :create_scope_for_association
18
+
19
+ configuration_defaults do |c|
20
+ c.auto_scopes_location = 'config/auto_scopes'
21
+ c.scope_association_prefix = 'by'
22
+ c.create_scope_for_association = true
23
+ end
24
+
25
+ class << self
26
+ def config
27
+ AutoScopes.configuration
28
+ end
29
+
30
+ def associations_source
31
+ @_associations_source ||= YAML.load_file(Rails.root.join(config.auto_scopes_location + '.yml')) || []
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,19 @@
1
+ require 'rails/generators/base'
2
+
3
+ module AutoScopes
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('config')
7
+
8
+ desc 'Create a default files for auto_scopes with some use cases'
9
+
10
+ def copy_initializer
11
+ copy_file 'initializers/auto_scopes.rb', 'config/initializers/auto_scopes.rb'
12
+ end
13
+
14
+ def copy_auto_scopes_yml
15
+ copy_file 'auto_scopes.yml', 'config/auto_scopes.yml'
16
+ end
17
+ end
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,201 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auto_scopes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - victor95pc
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.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: 3.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: configurations
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
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.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: appraisal
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: generator_spec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: I always wonder if it was possible to reuse some scopes defined in a
140
+ deep associations, we all know that is possible using merges but it still not a
141
+ clean solution, I still need to define new scopes and make joins so Rails can know
142
+ which model I want, so I started to develop this gem, the idea is easy to follow,
143
+ put all scopes you want inside a YAML file.
144
+ email:
145
+ - victorpalomocastro@gmail.com
146
+ executables: []
147
+ extensions: []
148
+ extra_rdoc_files: []
149
+ files:
150
+ - ".gitignore"
151
+ - ".rspec"
152
+ - ".travis.yml"
153
+ - Appraisals
154
+ - Gemfile
155
+ - LICENSE
156
+ - README.md
157
+ - Rakefile
158
+ - auto_scopes.gemspec
159
+ - bin/console
160
+ - bin/setup
161
+ - config/auto_scopes.yml
162
+ - config/initializers/auto_scopes.rb
163
+ - gemfiles/3.2.gemfile
164
+ - gemfiles/3.2.gemfile.lock
165
+ - gemfiles/4.0.gemfile
166
+ - gemfiles/4.0.gemfile.lock
167
+ - gemfiles/4.1.gemfile
168
+ - gemfiles/4.1.gemfile.lock
169
+ - gemfiles/4.2.gemfile
170
+ - gemfiles/4.2.gemfile.lock
171
+ - lib/auto_scopes.rb
172
+ - lib/auto_scopes/associations_chains.rb
173
+ - lib/auto_scopes/associations_methods.rb
174
+ - lib/auto_scopes/check_models.rb
175
+ - lib/auto_scopes/version.rb
176
+ - lib/generators/auto_scopes/install_generator.rb
177
+ homepage: https://github.com/victor95pc/auto-scopes
178
+ licenses: []
179
+ metadata: {}
180
+ post_install_message:
181
+ rdoc_options: []
182
+ require_paths:
183
+ - lib
184
+ required_ruby_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: 2.0.0
189
+ required_rubygems_version: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ requirements: []
195
+ rubyforge_project:
196
+ rubygems_version: 2.4.6
197
+ signing_key:
198
+ specification_version: 4
199
+ summary: Recycle already defined scopes on multiple models whatever the association
200
+ are, making your models way more DRYer
201
+ test_files: []