jordi-find_by_sql_file 0.9.4 → 0.9.5

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/Rakefile CHANGED
@@ -20,3 +20,17 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
20
20
  rdoc.rdoc_files.include('README')
21
21
  rdoc.rdoc_files.include('lib/**/*.rb')
22
22
  end
23
+
24
+ desc 'Measures test coverage using rcov'
25
+ task :rcov do
26
+ rm_f 'coverage'
27
+ rm_f 'coverage.data'
28
+
29
+ rcov = %{ rcov --aggregate coverage.data --text-summary
30
+ --include lib
31
+ --exclude /Library/Ruby
32
+ }.strip!.gsub! /\s+/, ' '
33
+
34
+ system("#{rcov} --html #{Dir.glob('test/**/*_test.rb').join(' ')}")
35
+ system('open coverage/index.html') if PLATFORM['darwin']
36
+ end
data/WHATSNEW ADDED
@@ -0,0 +1,6 @@
1
+ * What's new in version 0.9.5 (2008-10-06):
2
+
3
+ - Added a WHATSNEW file :)
4
+ - Added a few behaviour tests
5
+ - Added ActiveRecord::Base#find_one_by_sql(*args) as sugar for
6
+ ActiveRecord::Base#find_by_sql(*args).first
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'find_by_sql_file'
3
- s.version = '0.9.4'
4
- s.date = '2008-10-03'
3
+ s.version = '0.9.5'
4
+ s.date = '2008-10-06'
5
5
 
6
6
  s.author = 'Jordi Bunster'
7
7
  s.email = 'jordi@bunster.org'
@@ -14,21 +14,38 @@ Gem::Specification.new do |s|
14
14
  and ERB are available, and comments and indentation are
15
15
  stripped. }.strip!.gsub! /\s+/, ' '
16
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
- lib/find_by_sql_file.rb
29
- lib/jordi-find_by_sql_file.rb
30
- rails
31
- rails/init.rb ]
17
+ s.test_files = %w[
18
+ test
19
+ test/database.yml
20
+ test/find_by_sql_file_test.rb
21
+ test/mock_rails_root
22
+ test/mock_rails_root/app
23
+ test/mock_rails_root/app/queries
24
+ test/mock_rails_root/app/queries/articles
25
+ test/mock_rails_root/app/queries/articles/all.sql
26
+ test/mock_rails_root/app/queries/articles/count_all.sql
27
+ test/mock_rails_root/app/queries/articles/last_updated.sql
28
+ test/mock_rails_root/app/queries/articles/with_erb_fields.sql
29
+ test/mock_rails_root/app/queries/articles/with_variables.sql
30
+ test/schema.rb
31
+ test/test_helper.rb
32
+ ]
33
+
34
+ s.files = %w[ MIT-LICENSE
35
+ README.markdown
36
+ Rakefile
37
+ WHATSNEW
38
+ find_by_sql_file.gemspec
39
+ init.rb
40
+ install.rb
41
+ lib
42
+ lib/bunster
43
+ lib/bunster/erb_jacket.rb
44
+ lib/bunster/find_by_sql_file.rb
45
+ lib/find_by_sql_file.rb
46
+ lib/jordi-find_by_sql_file.rb
47
+ rails
48
+ rails/init.rb ]
32
49
 
33
50
  s.has_rdoc = true
34
51
  s.extra_rdoc_files = %w[ README.markdown ]
@@ -1,6 +1,7 @@
1
+ require 'bunster/erb_jacket'
2
+
1
3
  module Bunster # :nodoc:
2
4
  module FindBySqlFile
3
-
4
5
  # The path to the directory query queries will be found, organized
5
6
  # inside directories named like their main tables (or 'application'
6
7
  # for shared queries)
@@ -23,7 +24,7 @@ module Bunster # :nodoc:
23
24
  bound_variables = HashWithIndifferentAccess.new(opts).symbolize_keys!
24
25
  injected_locals = bound_variables.delete(:inject!) || []
25
26
 
26
- query = ERBJacket.wrap File.read(file_name), injected_locals
27
+ query = Bunster::ERBJacket.wrap File.read(file_name), injected_locals
27
28
  query = replace_named_bind_variables(query, bound_variables)
28
29
 
29
30
  query.gsub(/--.*/, '').squish!
@@ -34,5 +35,10 @@ module Bunster # :nodoc:
34
35
  query_or_symbol
35
36
  end
36
37
  end
38
+
39
+ # The equivalent of ActiveRecord::Base#find_by_sql(*args).first
40
+ def find_one_by_sql(*args)
41
+ find_by_sql(*args).first
42
+ end
37
43
  end
38
44
  end
data/test/database.yml ADDED
@@ -0,0 +1,3 @@
1
+ adapter: sqlite3
2
+ database: find_by_sql_file_plugin.sqlite3.db
3
+
@@ -1,8 +1,70 @@
1
1
  require 'test/unit'
2
+ require 'rubygems'
3
+
4
+ begin
5
+ require 'redgreen'
6
+ rescue LoadError
7
+ puts "Install the 'redgreen' gem to get color output"
8
+ end
9
+
10
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
11
 
3
12
  class FindBySqlFileTest < Test::Unit::TestCase
4
- # Replace this with your real tests.
5
- def test_this_plugin
6
- flunk
13
+
14
+ def test_counting_by_sql_file
15
+ assert_equal Article.count_by_sql(:count_all), Article.count
16
+ end
17
+
18
+ def test_find_all
19
+ assert_equal Article.find_by_sql(:all), Article.find(:all)
20
+ end
21
+
22
+ def test_find_last_updated
23
+ assert_equal Article.find_one_by_sql(:last_updated),
24
+ Article.find(:first, :order => 'updated_at DESC')
25
+ end
26
+
27
+ def test_named_bind_variables
28
+ assert_equal Article.find_by_sql(:with_variables,
29
+ :published => @articles.last.published,
30
+ :created_on => @articles.last.created_on),
31
+ Article.find(:all, :conditions => {
32
+ :published => @articles.last.published,
33
+ :created_on => @articles.last.created_on })
34
+ end
35
+
36
+ def test_erb_injection
37
+ fields = 'id, title, published'
38
+
39
+ assert_equal(
40
+ Article.find_by_sql(:with_erb_fields,
41
+ :inject! => { :select => fields }).map { |a| a.attributes },
42
+ Article.find(:all, :select => fields).map { |a| a.attributes } )
43
+ end
44
+
45
+ def setup
46
+ @articles = []
47
+
48
+ @articles << Article.new(:title => 'Lorem ipsum dolor sit amet',
49
+ :published => true,
50
+ :created_on => '1969-01-27',
51
+ :updated_at => '2008-10-06 12:02:32')
52
+
53
+ @articles << Article.new(:title => 'Consectetur adipisicing elit',
54
+ :published => false,
55
+ :created_on => '1969-12-30',
56
+ :updated_at => '2008-10-06 12:02:32')
57
+
58
+ @articles << Article.new(:title => 'Sed do eiusmod tempor incididunt',
59
+ :published => false,
60
+ :created_on => '1969-01-15',
61
+ :updated_at => '2008-10-06 12:02:32')
62
+
63
+ @articles << Article.new(:title => 'Ut enim ad minim veniam',
64
+ :published => false,
65
+ :created_on => '1999-12-13',
66
+ :updated_at => '2012-10-06 12:02:32')
67
+
68
+ @articles.each { |a| a.save }
7
69
  end
8
70
  end
@@ -0,0 +1,3 @@
1
+ -- This query gets all of the articles
2
+ SELECT * FROM articles
3
+
@@ -0,0 +1,2 @@
1
+ -- Count all of the articles using SQL
2
+ SELECT COUNT(*) FROM articles
@@ -0,0 +1,3 @@
1
+ SELECT * FROM articles
2
+ ORDER BY updated_at DESC -- Gets the last one updated
3
+ LIMIT 1
@@ -0,0 +1,3 @@
1
+ -- Gets certain fields from articles using ERB to pass the SELECT fragment
2
+
3
+ SELECT <%= @select -%> FROM articles
@@ -0,0 +1,4 @@
1
+ -- Gets articles with given values for 'created_on' and 'published'
2
+
3
+ SELECT * FROM articles WHERE created_on = :created_on
4
+ AND published = :published
data/test/schema.rb ADDED
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Schema.define :version => 1 do
2
+
3
+ create_table :articles, :force => true do |article|
4
+ article.string :title
5
+ article.boolean :published
6
+ article.date :created_on
7
+ article.timestamp :updated_at
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ require 'yaml'
2
+ require 'rubygems'
3
+ require 'active_record'
4
+
5
+ def establish_connection!
6
+ database_config_file = File.join File.dirname(__FILE__), 'database.yml'
7
+ database_configuration = YAML::load File.open(database_config_file)
8
+
9
+ ActiveRecord::Base.establish_connection database_configuration
10
+ end
11
+
12
+ def create_tables!
13
+ load File.join( File.dirname(__FILE__), 'schema.rb' )
14
+ end
15
+
16
+ class Article < ActiveRecord::Base
17
+ end
18
+
19
+ establish_connection!
20
+ create_tables!
21
+ RAILS_ROOT = File.join( File.dirname(__FILE__), 'mock_rails_root' )
22
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'find_by_sql_file')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jordi-find_by_sql_file
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.9.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordi Bunster
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-03 00:00:00 -07:00
12
+ date: 2008-10-06 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,6 +25,7 @@ files:
25
25
  - MIT-LICENSE
26
26
  - README.markdown
27
27
  - Rakefile
28
+ - WHATSNEW
28
29
  - find_by_sql_file.gemspec
29
30
  - init.rb
30
31
  - install.rb
@@ -65,4 +66,16 @@ specification_version: 2
65
66
  summary: Extension to ActiveRecord's find_by_sql to use SQL files
66
67
  test_files:
67
68
  - test
69
+ - test/database.yml
68
70
  - test/find_by_sql_file_test.rb
71
+ - test/mock_rails_root
72
+ - test/mock_rails_root/app
73
+ - test/mock_rails_root/app/queries
74
+ - test/mock_rails_root/app/queries/articles
75
+ - test/mock_rails_root/app/queries/articles/all.sql
76
+ - test/mock_rails_root/app/queries/articles/count_all.sql
77
+ - test/mock_rails_root/app/queries/articles/last_updated.sql
78
+ - test/mock_rails_root/app/queries/articles/with_erb_fields.sql
79
+ - test/mock_rails_root/app/queries/articles/with_variables.sql
80
+ - test/schema.rb
81
+ - test/test_helper.rb