basic_named_scopes 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -5,11 +5,14 @@ find-method as named scopes, for easy reusability and prettier code.
5
5
 
6
6
  Instead of writing:
7
7
 
8
- Post.all(:conditions => { :published => true }, :select => :title, :includes => :author)
8
+ Post.all(:conditions => { :published => true }, :select => :title, :include => :author)
9
9
 
10
10
  You can now write:
11
11
 
12
- Post.conditions(:published => true).select(:title).include(:author)
12
+ Post.conditions(:published => true).select(:title).with(:author)
13
+
14
+ All named scopes are called the same, except for +include+, which is now
15
+ called +with+, because +include+ is a reserved method.
13
16
 
14
17
  Reuse them by making class methods:
15
18
 
@@ -36,13 +39,15 @@ all, for greater flexibility.
36
39
 
37
40
  Arrays can be used as multple parameters too, sparing you some brackets.
38
41
 
39
- Post.include(:author, :comments).conditions("name LIKE ?", query)
42
+ Post.with(:author, :comments).conditions("name LIKE ?", query)
40
43
 
41
44
  The +read_only+ and +lock+ scopes default to true, but can be adjusted.
42
45
 
43
46
  Post.readonly # => same as Post.all(:readonly => true)
44
47
  Post.readonly(false) # => same as Post.all(:readonly => false)
45
48
 
49
+
50
+
46
51
  == Why?
47
52
 
48
53
  NamedScopes are really handy and they should play a more central theme in ActiveRecord.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -0,0 +1,56 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{basic_named_scopes}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Iain Hecker"]
12
+ s.date = %q{2009-12-19}
13
+ s.description = %q{Make your queries prettier and more reusable by having a named scope for every find-parameter. As easy as Post.include(:author, :comments)}
14
+ s.email = %q{iain@iain.nl}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ ".gitignore",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "basic_named_scopes.gemspec",
25
+ "lib/basic_named_scopes.rb",
26
+ "spec/basic_named_scopes_spec.rb",
27
+ "spec/spec.opts",
28
+ "spec/spec_helper.rb"
29
+ ]
30
+ s.homepage = %q{http://github.com/iain/basic_named_scopes}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.5}
34
+ s.summary = %q{Basic named scopes for ActiveRecord makes all find-parameters a named scope}
35
+ s.test_files = [
36
+ "spec/basic_named_scopes_spec.rb",
37
+ "spec/spec_helper.rb"
38
+ ]
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
45
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
46
+ s.add_development_dependency(%q<temping>, [">= 1.1.0"])
47
+ else
48
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
49
+ s.add_dependency(%q<temping>, [">= 1.1.0"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
+ s.add_dependency(%q<temping>, [">= 1.1.0"])
54
+ end
55
+ end
56
+
@@ -3,11 +3,14 @@
3
3
  #
4
4
  # Instead of writing:
5
5
  #
6
- # Post.all(:conditions => { :published => true }, :select => :title, :includes => :author)
6
+ # Post.all(:conditions => { :published => true }, :select => :title, :include => :author)
7
7
  #
8
8
  # You can now write:
9
9
  #
10
- # Post.conditions(:published => true).select(:title).include(:author)
10
+ # Post.conditions(:published => true).select(:title).with(:author)
11
+ #
12
+ # All named scopes are called the same, except for +include+, which is now
13
+ # called +with+, because +include+ is a reserved method.
11
14
  #
12
15
  # Reuse them by making class methods:
13
16
  #
@@ -34,7 +37,7 @@
34
37
  #
35
38
  # Arrays can be used as multple parameters too, sparing you some brackets.
36
39
  #
37
- # Post.include(:author, :comments).conditions("name LIKE ?", query)
40
+ # Post.with(:author, :comments).conditions("name LIKE ?", query)
38
41
  #
39
42
  # The +read_only+ and +lock+ scopes default to true, but can be adjusted.
40
43
  #
@@ -43,21 +46,55 @@
43
46
  #
44
47
  module BasicNamedScopes
45
48
 
46
- FIND_PARAMETERS = [:conditions, :order, :group, :having, :limit, :offset, :joins, :include, :select, :from]
49
+ # These are the normal parameters that will be turned into named scopes.
50
+ FIND_PARAMETERS = [:conditions, :order, :group, :having, :limit, :offset, :joins, :select, :from]
51
+
52
+ # These are the parameters that want a boolean.
47
53
  FIND_BOOLEAN_SWITCHES = [:readonly, :lock]
48
54
 
49
- def self.extended(record)
50
- FIND_PARAMETERS.each do |parameter|
51
- record.named_scope(parameter, lambda { |*args| { parameter => args.size == 1 ? args.first : args } })
55
+ class << self
56
+
57
+ # When you extend an ActiveRecord model (or Base for that matter) it will
58
+ # add the named scopes. This will automatically be done when the gem is
59
+ # loaded.
60
+ def extended(model)
61
+ apply_basic_named_scopes(model)
52
62
  end
53
- FIND_BOOLEAN_SWITCHES.each do |parameter|
54
- record.named_scope(parameter, lambda { |*args| { parameter => args.size == 0 ? true : args[0] } })
63
+
64
+ def apply_basic_named_scopes(model)
65
+ model.named_scope(:with, expand_into_array(:include))
66
+ FIND_PARAMETERS.each do |parameter|
67
+ model.named_scope(parameter, expand_into_array(parameter))
68
+ end
69
+ FIND_BOOLEAN_SWITCHES.each do |parameter|
70
+ model.named_scope(parameter, default_to_true(parameter))
71
+ end
72
+ model.named_scope(:all, default_to_empty_hash)
73
+ end
74
+
75
+ private
76
+
77
+ # The lambda for normal parameters. Will return an array when passed
78
+ # multiple parameters, saving you the brackets.
79
+ def expand_into_array(parameter)
80
+ lambda { |*args| { parameter => args.size == 1 ? args[0] : args } }
55
81
  end
56
- record.named_scope(:all, lambda { |*args| args[0] || {} })
82
+
83
+ # The lambda for boolean switches (readonly and lock). Will default
84
+ # to true when no argument has been given.
85
+ def default_to_true(parameter)
86
+ lambda { |*args| { parameter => args.size == 0 ? true : args[0] } }
87
+ end
88
+
89
+ # For the +all+-method, an empty hash will be the default when no
90
+ # parameters have been given. Otherwise, it just accepts just one
91
+ # parameter.
92
+ def default_to_empty_hash
93
+ lambda { |*args| args[0] || {} }
94
+ end
95
+
57
96
  end
58
97
 
59
98
  end
60
99
 
61
- if defined? ActiveRecord
62
- ActiveRecord::Base.extend(BasicNamedScopes)
63
- end
100
+ ActiveRecord::Base.extend(BasicNamedScopes) if defined?(ActiveRecord)
@@ -7,8 +7,15 @@ describe "BasicNamedScopes" do
7
7
  with_columns do |table|
8
8
  table.boolean :published
9
9
  table.boolean :visible
10
+ table.integer :author_id
10
11
  end
11
- extend BasicNamedScopes
12
+ belongs_to :author
13
+ end
14
+ create_model :authors do
15
+ with_columns do |table|
16
+ table.string :name
17
+ end
18
+ has_many :posts
12
19
  end
13
20
  end
14
21
 
@@ -37,7 +44,13 @@ describe "BasicNamedScopes" do
37
44
  subject.should == [ @published ]
38
45
  end
39
46
 
40
- [:conditions, :order, :group, :having, :limit, :offset, :joins, :include, :select, :from].each do |option|
47
+ it "should have a scope named 'with' but internally use 'include' as parameter" do
48
+ Post.create!(:author => Author.create!)
49
+ Author.should_receive(:find).and_return([])
50
+ Post.with(:author).to_a
51
+ end
52
+
53
+ [:conditions, :order, :group, :having, :limit, :offset, :joins, :with, :select, :from].each do |option|
41
54
  it "should known #{option} ActiveRecord::Base.find" do
42
55
  Post.send(option).class.should be_scope
43
56
  end
data/spec/spec_helper.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'basic_named_scopes'
4
3
  require 'spec'
5
4
  require 'spec/autorun'
6
5
  require 'active_record'
7
6
  require 'temping'
7
+ require 'basic_named_scopes'
8
8
 
9
9
  Spec::Runner.configure do |config|
10
10
  config.include(Temping)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: basic_named_scopes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Iain Hecker
@@ -46,6 +46,7 @@ files:
46
46
  - README.rdoc
47
47
  - Rakefile
48
48
  - VERSION
49
+ - basic_named_scopes.gemspec
49
50
  - lib/basic_named_scopes.rb
50
51
  - spec/basic_named_scopes_spec.rb
51
52
  - spec/spec.opts