jordi-find_by_sql_file 0.9.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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2008 Jordi Bunster <jordi@bunster.org>
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.
data/README.markdown ADDED
@@ -0,0 +1,55 @@
1
+ # Find by SQL file
2
+
3
+ This plugin extends the API of `ActiveRecord::Base.find_by_sql`.
4
+
5
+ ## Usage
6
+
7
+ Instead of passing the SQL statement as a string...
8
+
9
+ Elephant.find_by_sql "SELECT * FROM elephants WHERE weight='massive'"
10
+
11
+ You can pass a symbol that refers to a query file stored in
12
+ `RAILS_ROOT`/app/queries/`TABLE_NAME`/`SYMBOL`.sql
13
+
14
+ Elephant.find_by_sql :massive_weight
15
+
16
+ ## A Warning
17
+
18
+ Besides the warnings on the `ERB` section below (don't ignore those), this
19
+ code is not tested at all, and has no track record whatsoever. So there,
20
+ beware.
21
+
22
+ ## Motivation
23
+
24
+ The advantage of the latter approach is that the SQL file can be properly
25
+ indented and commented (the indentation and comments are stripped from the
26
+ logs.)
27
+
28
+ It's possible to pass named bind variables, much like in the conditions
29
+ parameter of `ActiveRecord::Base.find`, by passing a hash as the second
30
+ parameter, like so:
31
+
32
+ Elephant.find_by_sql :specifics, :color => 'grey', :weight => 6800
33
+
34
+ ## ERB (be careful)
35
+
36
+ It is also possible to use `ERB` inside the query file, but **beware!**
37
+ Unlike the named bind variables, any data passed in via the ERB method is not
38
+ properly quoted by the database adapter, leaving open the possibility of
39
+ **SQL injection**. 99.9% of the time, **you will _NOT_ need this**.
40
+
41
+ Here's an artificial (but easy to explain) example of how the
42
+ (very dangerous!) `ERB` _feature_ works:
43
+
44
+ Elephant.find_by_sql :single_value, :value => 'grey',
45
+ :inject! => { :field => 'color' }
46
+
47
+ The call above replaces the bind variable `:value` inside the SQL file,
48
+ but it also populates the instance variable `@field` with `"color"`, which
49
+ can then be used with the usual ERB syntax, like so:
50
+
51
+ SELECT <%= @field -%> FROM elephants WHERE <%= @field -%> = :value
52
+
53
+ ### Legal
54
+
55
+ Copyright (c) 2008 Jordi Bunster, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the find_by_sql_file plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the find_by_sql_file plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'FindBySqlFile'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,34 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'find_by_sql_file'
3
+ s.version = '0.9.0'
4
+ s.date = '2008-10-02'
5
+
6
+ s.author = 'Jordi Bunster'
7
+ s.email = 'jordi@bunster.org'
8
+ s.homepage = 'http://github.com/jordi/find_by_sql_file'
9
+
10
+ s.summary = "Extension to ActiveRecord's find_by_sql to use SQL files"
11
+ s.description = %{ This ActiveRecord extension lets you pass a symbol to
12
+ ActiveRecord::Base#find_by_sql, which refers to a query
13
+ file saved under app/queries/. Variable replacement
14
+ and ERB are available, and comments and indentation are
15
+ stripped. }.strip!.gsub! /\s+/, ' '
16
+
17
+ s.test_files = %w[ test test/find_by_sql_file_test.rb ]
18
+ s.files = %w[ MIT-LICENSE
19
+ README.markdown
20
+ Rakefile
21
+ find_by_sql_file.gemspec
22
+ init.rb
23
+ install.rb
24
+ lib
25
+ lib/bunster
26
+ lib/bunster/erb_jacket.rb
27
+ lib/bunster/find_by_sql_file.rb
28
+ rails
29
+ rails/init.rb ]
30
+
31
+ s.has_rdoc = true
32
+ s.extra_rdoc_files = %w[ README.markdown ]
33
+ s.rdoc_options = %w[ --main README.markdown ]
34
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/rails/init'
data/install.rb ADDED
@@ -0,0 +1,5 @@
1
+ unless File.exist?(Bunster::FindBySqlFile::SQL_PATH) &&
2
+ File.directory?(Bunster::FindBySqlFile::SQL_PATH)
3
+
4
+ FileUtils.mkdir_p Bunster::FindBySqlFile::SQL_PATH
5
+ end
@@ -0,0 +1,23 @@
1
+ require 'erb'
2
+
3
+ module Bunster # :nodoc:
4
+
5
+ # This makes it a tad simpler to create a clean ERB binding context. It
6
+ # works like so:
7
+ #
8
+ # ERBJacket.wrap "A Foo is: <%= @foo -%>", :foo => 'not a bar'
9
+ #
10
+ class ERBJacket
11
+ def initialize(hash) # :nodoc:
12
+ hash.each { |k, v| instance_variable_set('@' + k.to_s, v) }
13
+ end
14
+
15
+ def wrap(erb_template) # :nodoc:
16
+ ::ERB.new(erb_template, nil, '-').result binding
17
+ end
18
+
19
+ def self.wrap(erb_template = '', instance_variables = {}) # :nodoc:
20
+ new(instance_variables).wrap erb_template
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,38 @@
1
+ module Bunster # :nodoc:
2
+ module FindBySqlFile
3
+
4
+ # The path to the directory query queries will be found, organized
5
+ # inside directories named like their main tables (or 'application'
6
+ # for shared queries)
7
+ SQL_PATH = File.join RAILS_ROOT, 'app', 'queries'
8
+
9
+ def find_by_sql_with_sql_file(query_or_symbol, opts = {}) # :nodoc:
10
+ find_by_sql_without_sql_file sql_file(query_or_symbol, opts)
11
+ end
12
+
13
+ def count_by_sql_with_sql_file(query_or_symbol, opts = {}) # :nodoc:
14
+ count_by_sql_without_sql_file sql_file(query_or_symbol, opts)
15
+ end
16
+
17
+ def sql_file(query_or_symbol, opts = {}) # :nodoc:
18
+ if query_or_symbol.is_a? Symbol
19
+
20
+ file_name = File.join SQL_PATH,
21
+ (table_name rescue 'application'), (query_or_symbol.to_s + '.sql')
22
+
23
+ bound_variables = HashWithIndifferentAccess.new(opts).symbolize_keys!
24
+ injected_locals = bound_variables.delete(:inject!) || []
25
+
26
+ query = ERBJacket.wrap File.read(file_name), injected_locals
27
+ query = replace_named_bind_variables(query, bound_variables)
28
+
29
+ query.gsub(/--.*/, '').squish!
30
+ else
31
+ raise %' Additional parameters only supported when using a query
32
+ file (pass a symbol, not a string) '.squish! unless opts.blank?
33
+
34
+ query_or_symbol
35
+ end
36
+ end
37
+ end
38
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'bunster/find_by_sql_file'
2
+
3
+ class << ActiveRecord::Base
4
+ include Bunster::FindBySqlFile
5
+
6
+ alias_method_chain :find_by_sql, :sql_file
7
+ alias_method_chain :count_by_sql, :sql_file
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+
3
+ class FindBySqlFileTest < Test::Unit::TestCase
4
+ # Replace this with your real tests.
5
+ def test_this_plugin
6
+ flunk
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jordi-find_by_sql_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Jordi Bunster
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-02 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: This ActiveRecord extension lets you pass a symbol to ActiveRecord::Base#find_by_sql, which refers to a query file saved under app/queries/. Variable replacement and ERB are available, and comments and indentation are stripped.
17
+ email: jordi@bunster.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.markdown
24
+ files:
25
+ - MIT-LICENSE
26
+ - README.markdown
27
+ - Rakefile
28
+ - find_by_sql_file.gemspec
29
+ - init.rb
30
+ - install.rb
31
+ - lib
32
+ - lib/bunster
33
+ - lib/bunster/erb_jacket.rb
34
+ - lib/bunster/find_by_sql_file.rb
35
+ - rails
36
+ - rails/init.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/jordi/find_by_sql_file
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --main
42
+ - README.markdown
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.2.0
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Extension to ActiveRecord's find_by_sql to use SQL files
64
+ test_files:
65
+ - test
66
+ - test/find_by_sql_file_test.rb