association-reporter 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b63e95394a41821ded5e277c6d34edec80de0ad1
4
+ data.tar.gz: 6d368760d3fb6d645dc60b09244681925d046c9c
5
+ SHA512:
6
+ metadata.gz: 906fcd7a3d40e04c5822d449fed3e342b9d9ea265133d8f3b162739c8a8e37f3c4f16eecdcfc30ea68dcf7d0a3ce3ee66d7572ba6f52707f2dc672db9378f1dc
7
+ data.tar.gz: b91e691ed08fd9b4472560bf56935545fcbc60e184498eaa459ab548ee2240bfd3d514461fd7bb03bea5da92c30ace8bef5ab1c3f4d058d699cad4304f1bfd4e
@@ -0,0 +1,5 @@
1
+ .DS_Store
2
+ .irb-history
3
+ repl.db
4
+ test.db
5
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Matt Baker
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.
@@ -0,0 +1,3 @@
1
+ # Association Reporter
2
+
3
+ Hella WIP
@@ -0,0 +1,19 @@
1
+ require 'bundler/setup'
2
+
3
+
4
+ begin
5
+ require 'rspec/core/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec) do |task_config|
8
+ task_config.verbose = false
9
+ end
10
+
11
+ task :default => :spec
12
+ rescue LoadError
13
+ end
14
+
15
+
16
+ desc 'Play with the spec database'
17
+ task "play" do
18
+ exec "irb -r./repl/config"
19
+ end
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = 'association-reporter'
3
+ spec.version = '0.0.1'
4
+ spec.date = '2016-04-01'
5
+ spec.summary = "Tell me about your has_many"
6
+ spec.description = "Detailed reports for Active Record Associations"
7
+ spec.authors = ["Matt Baker"]
8
+ spec.email = 'mbaker.pdx@gmail.com'
9
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
10
+ f.match(%r{^spec|repl})
11
+ end
12
+ spec.homepage = 'https://github.com/mattbaker/association-reporter'
13
+ spec.license = 'MIT'
14
+
15
+ spec.add_development_dependency 'bundler', '~>1.9'
16
+ spec.add_development_dependency 'rake', '~>10.5'
17
+ spec.add_development_dependency 'rspec', '~>3.4'
18
+ spec.add_development_dependency 'sqlite3', '~>1.3'
19
+ spec.add_development_dependency 'activerecord','~>4.2'
20
+ spec.add_runtime_dependency 'colorize', '~>0.7'
21
+ end
@@ -0,0 +1,4 @@
1
+ module AssociationReporter
2
+ PROJECT_ROOT = File.expand_path('../../', __FILE__)
3
+ TEMPLATES = File.join(PROJECT_ROOT, "templates")
4
+ end
@@ -0,0 +1,126 @@
1
+ require 'association-reporter'
2
+ require 'singleton'
3
+
4
+ module AssociationReporter
5
+ class Assumption
6
+ def initialize(reflection)
7
+ @reflection = reflection
8
+ end
9
+
10
+ private
11
+ attr_reader :reflection
12
+
13
+ def labelize(str, cond)
14
+ str && str.to_s.colorize(cond ? :green : :red)
15
+ end
16
+ end
17
+
18
+
19
+ class HasManyThroughAssociationAssumption < Assumption
20
+ def through_name
21
+ reflection.options[:through].to_s
22
+ end
23
+
24
+ def valid_through_method?
25
+ reflection.active_record.method_defined?(through_name)
26
+ end
27
+
28
+ def through_name_label
29
+ labelize(reflection.options[:through], valid_through_method?)
30
+ end
31
+
32
+ def through_assumption
33
+ reflection.through_reflection &&
34
+ AssociationAssumption.new(reflection.through_reflection)
35
+ end
36
+
37
+ def source_name
38
+ reflection.source_reflection_names.first.to_s
39
+ end
40
+
41
+ def source_label
42
+ labelize(source_name, reflection.source_reflection)
43
+ end
44
+
45
+ def source_assumption
46
+ reflection.source_reflection &&
47
+ AssociationAssumption.new(reflection.source_reflection)
48
+ end
49
+
50
+ def valid?
51
+ through_assumption.try(:valid?) && source_assumption.try(:valid?)
52
+ end
53
+ end
54
+
55
+ class AssociationAssumption < Assumption
56
+ def assoc_klass_name
57
+ reflection.class_name
58
+ end
59
+
60
+ def assoc_klass
61
+ begin
62
+ return reflection.klass
63
+ rescue NameError => e
64
+ raise e if assoc_klass_name && !e.name.include?(assoc_klass_name)
65
+ end
66
+ nil
67
+ end
68
+
69
+ def assoc_klass_label
70
+ labelize(assoc_klass_name, assoc_klass)
71
+ end
72
+
73
+ def assoc_table
74
+ assoc_klass.try(:table_name) || assoc_klass_name.underscore.downcase.pluralize
75
+ end
76
+
77
+ def assoc_table_label
78
+ labelize(assoc_table, valid_table?)
79
+ end
80
+
81
+ def valid_table?
82
+ ActiveRecord::Base.connection.table_exists?(assoc_table)
83
+ end
84
+
85
+ def valid?
86
+ valid_table? && assoc_klass
87
+ end
88
+
89
+ private
90
+ attr_reader :reflection
91
+ end
92
+
93
+ class OneToManyAssociationAssumption < AssociationAssumption
94
+ def fkey_table
95
+ if reflection.is_a?(ActiveRecord::Reflection::BelongsToReflection)
96
+ table_name = reflection.active_record.table_name
97
+ else
98
+ table_name = assoc_table
99
+ end
100
+ end
101
+
102
+ def fkey_table_label
103
+ labelize(fkey_table, valid_fkey_table?)
104
+ end
105
+
106
+ def fkey_column
107
+ reflection.foreign_key
108
+ end
109
+
110
+ def fkey_column_label
111
+ labelize(fkey_column, valid_fkey_table? && valid_fkey_column?)
112
+ end
113
+
114
+ def valid_fkey_table?
115
+ ActiveRecord::Base.connection.table_exists?(fkey_table)
116
+ end
117
+
118
+ def valid_fkey_column?
119
+ ActiveRecord::Base.connection.column_exists?(fkey_table, fkey_column)
120
+ end
121
+
122
+ def valid?
123
+ super && valid_fkey_table? && valid_fkey_column?
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,38 @@
1
+ require 'colorize'
2
+ require 'association-reporter'
3
+ require 'association-reporter/template'
4
+ require 'association-reporter/assumption'
5
+
6
+ module AssociationReporter
7
+ module Reporter
8
+
9
+ def describe(association)
10
+ reflection = self.reflect_on_association(association)
11
+ raise "Can't find an association #{self.name}##{association}" if reflection.nil?
12
+
13
+ if reflection.is_a? ActiveRecord::Reflection::ThroughReflection
14
+ puts many_to_many_description(reflection)
15
+ else
16
+ puts one_to_many_description(reflection)
17
+ end
18
+ end
19
+
20
+ def many_to_many_description(reflection)
21
+ template_vars = {
22
+ reflection: reflection,
23
+ assumption: AssociationReporter::HasManyThroughAssociationAssumption.new(reflection)
24
+ }
25
+ AssociationReporter::Template.render('many_to_many.erb', template_vars)
26
+ end
27
+
28
+ def one_to_many_description(reflection)
29
+ template_vars = {
30
+ reflection: reflection,
31
+ assumption: AssociationReporter::OneToManyAssociationAssumption.new(reflection)
32
+ }
33
+
34
+ AssociationReporter::Template.render('one_to_many.erb', template_vars)
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,16 @@
1
+ require 'association-reporter'
2
+ require 'ostruct'
3
+ require 'erb'
4
+
5
+ module AssociationReporter
6
+ class Template < OpenStruct
7
+ def render(template, template_directory=AssociationReporter::TEMPLATES)
8
+ templ_string = File.read(File.join(template_directory, template))
9
+ ERB.new(templ_string, 0, '-').result(binding)
10
+ end
11
+
12
+ def self.render(template, locals)
13
+ Template.new(locals).render(template)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ Describing the association <%= reflection.active_record %>#<%= reflection.name %>:
2
+
3
+ This '<%= reflection.macro %>' association is looking for another association called <%= reflection.active_record %>#<%= assumption.through_name_label %> to jump through.
4
+
5
+ <% if assumption.through_assumption -%>
6
+ It will go through the association <%= reflection.active_record %>#<%= assumption.through_name_label %> to the model <%= assumption.through_assumption.assoc_klass_label %>.
7
+ <% if assumption.through_assumption.assoc_klass -%>
8
+ When it gets to <%= assumption.through_assumption.assoc_klass_label %> it <%#
9
+ %><% if assumption.source_assumption -%>
10
+ will jump through a source association called <%= assumption.through_assumption.assoc_klass_label %>#<%= assumption.source_label %>.
11
+
12
+ <%= assumption.through_assumption.assoc_klass_label %>#<%= assumption.source_label %> is an association that returns <%= assumption.source_assumption.assoc_klass_label %> objects from the <%= assumption.source_assumption.assoc_table_label %> table.
13
+ <% else -%>
14
+ will try to jump through an association called <%= assumption.through_assumption.assoc_klass_label%>#<%= assumption.source_label %>.
15
+ <% end -%>
16
+ <% end -%>
17
+ <% end -%>
18
+
19
+ This association is feeling <%= assumption.valid? ? '😃' : '😢' %>
@@ -0,0 +1,6 @@
1
+ Describing the association <%= reflection.active_record %>#<%= reflection.name %>:
2
+
3
+ This '<%= reflection.macro %>' association is looking for a column called '<%= assumption.fkey_column_label %>' on the table '<%= assumption.fkey_table_label %>'.
4
+ It's expecting to return <%= reflection.collection? ? "a collection of" : "a"%> <%= assumption.assoc_klass_label %> object<%= reflection.collection? ? "s" : "" %> from the <%= assumption.assoc_table_label %> table.
5
+
6
+ This association is feeling <%= assumption.valid? ? '😃' : '😢' %>
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: association-reporter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Baker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: colorize
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.7'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.7'
97
+ description: Detailed reports for Active Record Associations
98
+ email: mbaker.pdx@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".gitignore"
104
+ - Gemfile
105
+ - Gemfile.lock
106
+ - LICENSE
107
+ - README.md
108
+ - Rakefile
109
+ - association-reporter.gemspec
110
+ - lib/association-reporter.rb
111
+ - lib/association-reporter/assumption.rb
112
+ - lib/association-reporter/reporter.rb
113
+ - lib/association-reporter/template.rb
114
+ - templates/many_to_many.erb
115
+ - templates/one_to_many.erb
116
+ homepage: https://github.com/mattbaker/association-reporter
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.4.5
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Tell me about your has_many
140
+ test_files: []