obviously 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE.txt +19 -0
  2. data/README.md +44 -0
  3. data/lib/obviously.rb +21 -0
  4. metadata +98 -0
@@ -0,0 +1,19 @@
1
+ The MIT License (MIT)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ [Donate to charity: water via Gittip](https://www.gittip.com/nthj/)
2
+
3
+ obviously
4
+ =========
5
+
6
+ Guess obvious association class names in ActiveRecord.
7
+
8
+ Makes using namespaced model names, like Project::Discussion, much less painful.
9
+
10
+ Installation
11
+ ============
12
+
13
+ Add this to your Gemfile:
14
+
15
+ gem 'obviously'
16
+
17
+ Usage
18
+ =====
19
+
20
+ Notice the lack of `class_name: 'User::Membership'` style options in the associations. Works with `belongs_to`, `has_one` and `has_many`:
21
+
22
+ class Project < ActiveRecord::Base
23
+ class Attachment < ActiveRecord::Base
24
+ belongs_to :discussion
25
+ # class_name: 'Project::Discussion'
26
+ end
27
+
28
+ class Discussion < ActiveRecord::Base
29
+ belongs_to :project
30
+
31
+ has_many :attachments
32
+ # class_name: 'Project::Attachment'
33
+ end
34
+
35
+ has_many :discussions
36
+ # class_name: 'Project::Discussion'
37
+
38
+ has_many :attachments,
39
+ through: :discussions
40
+ # class_name: 'Project::Attachment'
41
+ end
42
+
43
+
44
+
@@ -0,0 +1,21 @@
1
+ require 'active_record'
2
+
3
+ class ActiveRecord::Reflection::AssociationReflection
4
+ class AmbiguousAssociationClassError < Exception
5
+ def initialize results
6
+ super "Could not guess association, please specify using class_name. Likely options: #{results.join(', ')}"
7
+ end
8
+ end
9
+
10
+ def klass
11
+ super
12
+ rescue NameError
13
+ @klass ||= ActiveRecord::Base.descendants.select do |model|
14
+ model.name.demodulize.parameterize('_') == class_name.parameterize('_')
15
+ end.tap do |results|
16
+ if results.map(&:name).uniq.many?
17
+ raise AmbiguousAssociationClassError.new(results.map(&:name).uniq)
18
+ end
19
+ end.first
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: obviously
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nathaniel Jones
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-04-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '4'
30
+ - !ruby/object:Gem::Dependency
31
+ name: sqlite3
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 2.0.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.0
62
+ description: ! 'Makes using namespaced model names, like Project::Discussion, much
63
+ less painful. No more `class_name: ''Project::Discussion''` options required.'
64
+ email:
65
+ - nj@third.io
66
+ - ''
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - lib/obviously.rb
72
+ - README.md
73
+ - LICENSE.txt
74
+ homepage: http://github.com/nthj/obviously
75
+ licenses: []
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: 1.3.6
92
+ requirements: []
93
+ rubyforge_project: obviously
94
+ rubygems_version: 1.8.23
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Guess obvious association class names in ActiveRecord.
98
+ test_files: []