interrogator 1.0.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.
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in interrogator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ == MIT License
2
+
3
+ Copyright (c) 2010, Jim Gay.
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,11 @@
1
+ # Interrogator
2
+
3
+ Use this Rails Engine to get information about your classes.
4
+
5
+ You'll be able to access `/interrogator.json?klass=SomeModel` to get
6
+ details about the `klass` that you requested.
7
+ It'll return JSON with details about the model's columns and it's
8
+ associated objects.
9
+
10
+ If you have Searchlogic installed you'll get JSON options for
11
+ querying from `/interrogator.js`
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,25 @@
1
+ class InterrogatorController < ApplicationController
2
+ def show
3
+ respond_to do |format|
4
+ format.json do
5
+ param_class = params[:klass].to_s.camelize(:upper)
6
+ details = {}
7
+ if param_class.present?
8
+ klass = param_class.constantize
9
+ details[:columns] = klass.simple_columns_array
10
+ details[:associations] = klass.associated_models_hash
11
+ end
12
+ render :json => details
13
+ end
14
+ format.js do
15
+ query_options = {}
16
+ query_options.tap do |hash|
17
+ Interrogator::Conditions.constants.each do |konstant|
18
+ hash[konstant.to_sym] = Interrogator::Conditions.const_get(konstant.to_sym)
19
+ end
20
+ end
21
+ render :json => query_options
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ map.resource :interrogator, :only => :show, :controller => 'interrogator'
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "interrogator/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "interrogator"
7
+ s.version = Interrogator::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jim Gay"]
10
+ s.email = ["jim@saturnflyer.com"]
11
+ s.homepage = "http://github.com/saturnflyer/interrogator"
12
+ s.summary = %q{Interrogate an ActiveRecord class to get details about attributes and associations.}
13
+ s.description = %q{Ask an ActiveRecord class for details about the database columns and details about the defined associations.}
14
+
15
+ s.rubyforge_project = "interrogator"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,39 @@
1
+ require 'active_record'
2
+ begin
3
+ require 'searchlogic'
4
+ rescue LoadError
5
+ STDERR.puts "Interrogator uses Searchlogic to provide query options. You'll need to define these yourself if you don't intend to use Searchlogic"
6
+ end
7
+ module Interrogator
8
+ def simple_columns_array(options={})
9
+ @interrogator_columns_hash ||= columns_hash
10
+ sort = options[:sort]||true
11
+ excl_columns = options[:except]||[]
12
+ incl_columns = options[:only]||[]
13
+ res=[]
14
+ @interrogator_columns_hash.each { |k, v|
15
+ ks=k.to_sym
16
+ next if excl_columns.include?(ks) || (!incl_columns.empty? && !incl_columns.include?(ks))
17
+ res << {
18
+ :column_name => k,
19
+ :type => v.type,
20
+ :limit => v.limit,
21
+ :null => v.null
22
+ }
23
+ }
24
+ sort ? res.sort{|a,b| a[:column_name] <=> b[:column_name]} : res
25
+ end
26
+
27
+ def associated_models_hash
28
+ {}.tap do |hash|
29
+ [:has_many, :has_one, :belongs_to].each do |assoc|
30
+ hash[assoc] = []
31
+ reflect_on_all_associations(assoc).each do |reflection|
32
+ hash[assoc] << {:name => reflection.name, :options => reflection.options.tap{|o| o.delete(:extend)}}
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ ActiveRecord::Base.send(:extend, Interrogator)
@@ -0,0 +1,11 @@
1
+ module Interrogator
2
+ class Conditions
3
+ if defined?(Searchlogic)
4
+ Searchlogic::NamedScopes::Conditions.constants.each do |konstant|
5
+ konst_sym = konstant.intern
6
+ konst_val = Searchlogic::NamedScopes::Conditions.const_get(konst_sym)
7
+ const_set(konst_sym, konst_val)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Interrogator
2
+ VERSION = "1.0.0"
3
+ end
File without changes
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: interrogator
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Jim Gay
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-17 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Ask an ActiveRecord class for details about the database columns and details about the defined associations.
23
+ email:
24
+ - jim@saturnflyer.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - app/controllers/interrogator_controller.rb
38
+ - config/routes.rb
39
+ - interrogator.gemspec
40
+ - lib/interrogator.rb
41
+ - lib/interrogator/conditions.rb
42
+ - lib/interrogator/version.rb
43
+ - rails/init.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/saturnflyer/interrogator
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project: interrogator
74
+ rubygems_version: 1.3.7
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Interrogate an ActiveRecord class to get details about attributes and associations.
78
+ test_files: []
79
+