gravtastic 1.7.1 → 2.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.
data/README.textile CHANGED
@@ -12,17 +12,21 @@ Somewhere in your application you need to @require 'gravtastic'@. In Rails >= 2.
12
12
 
13
13
  config.gem 'gravtastic'
14
14
 
15
- The next step is to give your model a Gravatar:
15
+ If you are using Merb & DataMapper you can add do the same by adding the following to your init.rb file:
16
+
17
+ dependency 'gravtastic'
18
+
19
+ Make sure you add this after either the @use_orm :datamapper@ line or after a DataMapper extension. The next step is to give your model a Gravatar:
16
20
 
17
21
  class User
18
- has_gravatar
22
+ is_gravtastic
19
23
  end
20
24
 
21
- If you are using a standard Ruby class or Datamapper resource you have to add the line @include Gravtastic::Model@ before @has_gravatar@.
25
+ If you are using a standard Ruby class you have to add the line @include Gravtastic::Resource@ before @is_gravtastic@.
22
26
 
23
27
  This defaults to looking for the gravatar ID on the @email@ method. So, if your @User@ has an @email@ then it will send that to Gravatar to get their picture. You can change the default gravatar source like this:
24
28
 
25
- has_gravatar :on => :author_email
29
+ is_gravtastic :with => :author_email
26
30
 
27
31
  Now, you can access your object's gravatar with the @gravatar_url@ method:
28
32
 
@@ -39,9 +43,9 @@ Note that it defaults to a PG rating. You can specify extra options with a hash:
39
43
 
40
44
  However, to DRY things up you can specify defaults in the class declaration.
41
45
 
42
- has_gravatar :author_email, :defaults => { :rating => 'R18', :size => 20 }
46
+ is_gravtastic :with => :author_email, :rating => 'R18', :size => 20
43
47
 
44
- Nice, now all the calls to gravatar_url will have the defaults you have specified.
48
+ Nice, now all the calls to gravatar_url will have the defaults you have specified. Any options that you pass when you use @gravatar_url@ will over-ride these class-defaults.
45
49
 
46
50
  PS. Bonus points for anybody who can tell me what that users email is!
47
51
 
data/Rakefile CHANGED
@@ -1,11 +1,12 @@
1
1
  require 'rubygems'
2
2
  require 'rake/gempackagetask'
3
+ require 'rake/rdoctask'
3
4
  require 'rubygems/specification'
4
5
  require 'spec/rake/spectask'
5
6
  require 'date'
6
7
 
7
8
  GEM = "gravtastic"
8
- GEM_VERSION = "1.7.1"
9
+ GEM_VERSION = "2.0.0"
9
10
  AUTHOR = "Chris Lloyd"
10
11
  EMAIL = "christopher.lloyd@gmail.com"
11
12
  HOMEPAGE = "http://github.com/chrislloyd/gravtastic"
@@ -31,10 +32,19 @@ spec = Gem::Specification.new do |s|
31
32
  s.files = %w(LICENSE README.textile Rakefile) + Dir.glob("{lib,spec}/**/*")
32
33
  end
33
34
 
35
+ desc "Run Specs"
34
36
  Rake::GemPackageTask.new(spec) do |pkg|
35
37
  pkg.gem_spec = spec
36
38
  end
37
39
 
40
+ desc "Generate Documentation"
41
+ Rake::RDocTask.new do |rdoc|
42
+ rdoc.rdoc_dir = 'doc'
43
+ rdoc.title = 'Gravtastic'
44
+ rdoc.options << '--line-numbers' << '--inline-source' << '--main' << 'Gravtastic'
45
+ rdoc.rdoc_files.include(FileList[ 'lib/**/*.rb', 'README.textile', 'LICENSE'])
46
+ end
47
+
38
48
  task :default => :spec
39
49
  task :specs => :spec
40
50
 
data/lib/gravtastic.rb CHANGED
@@ -12,19 +12,23 @@ require 'cgi'
12
12
  #
13
13
  # Somewhere in your application you need to <tt>require 'gravtastic'</tt>. In Rails >= 2.1 you can forego this and just add a dependency gem dependency for Gravtastic.
14
14
  #
15
- # config.gem 'gravtastic', :source => 'http://gems.github.com/'
15
+ # config.gem 'gravtastic'
16
16
  #
17
- # The next step is to give your model a Gravatar:
17
+ # If you are using Merb & DataMapper you can add do the same by adding the following to your init.rb file:
18
+ #
19
+ # dependency 'gravtastic'
20
+ #
21
+ # Make sure you add this after either the <tt>use_orm :datamapper</tt> line or after a DataMapper extension. The next step is to give your model a Gravatar:
18
22
  #
19
23
  # class User
20
- # has_gravatar
24
+ # is_gravtastic
21
25
  # end
22
26
  #
23
- # If you are using a standard Ruby class or Datamapper resource you have to add the line <tt>include Gravtastic::Model</tt> before <tt>has_gravatar</tt>.
24
- #
27
+ # If you are using a standard Ruby class you have to add the line <tt>include Gravtastic::Resource</tt> before <tt>is_gravtastic</tt>.
28
+ #
25
29
  # This defaults to looking for the gravatar ID on the <tt>email</tt> method. So, if your <tt>User</tt> has an <tt>email</tt> then it will send that to Gravatar to get their picture. You can change the default gravatar source like this:
26
30
  #
27
- # has_gravatar :on => :author_email
31
+ # is_gravtastic :with => :author_email
28
32
  #
29
33
  # Now, you can access your object's gravatar with the <tt>gravatar_url</tt> method:
30
34
  #
@@ -35,53 +39,62 @@ require 'cgi'
35
39
  #
36
40
  # current_user.gravatar_url(:rating => 'R18', :size => 512)
37
41
  # => "http://gravatar.com/e9e719b44653a9300e1567f09f6b2e9e.png?r=R18&s=512"
38
- #
42
+ #
39
43
  # current_user.gravatar_url(:secure => true)
40
44
  # => "https://secure.gravatar.com/e9e719b44653a9300e1567f09f6b2e9e.png?r=PG"
45
+ #
46
+ # However, to DRY things up you can specify defaults in the class declaration.
47
+ #
48
+ # is_gravtastic :with => :author_email, :rating => 'R18', :size => 20
49
+ #
50
+ # Nice, now all the calls to gravatar_url will have the defaults you have specified. Any options that you pass when you use <tt>gravatar_url</tt> will over-ride these class-defaults.
41
51
  #
42
52
  module Gravtastic
43
- module Model
53
+ module Resource
44
54
 
45
55
  def self.included(base) # :nodoc:
46
56
  base.extend(ClassMethods)
47
57
  end
48
-
58
+
49
59
  module ClassMethods
50
-
60
+
51
61
  #
52
62
  # Sets the gravatar_source. Basically starts this whole shindig.
53
63
  #
54
64
  # Examples:
55
65
  #
56
- # has_gravatar
66
+ # is_gravtastic
57
67
  #
58
- # has_gravatar :on => :author_email
68
+ # is_gravtastic :with => :author_email
59
69
  #
60
- # has_gravatar :defaults => { :rating => 'R18' }
70
+ # is_gravtastic :with => :author_email, :rating => 'R18', :secure => true
61
71
  #
62
- def has_gravatar(options={})
63
- @gravatar_source = options[:on] || :email
64
- options[:defaults] ||= {}
65
- @gravatar_defaults = {:rating => 'PG', :secure => false}.merge(options[:defaults])
72
+ def is_gravtastic(options={})
73
+ @gravatar_source = options[:with] || :email
74
+
75
+ options.delete_if {|key,_| ![:size, :rating, :default, :secure].include?(key) }
76
+ @gravatar_defaults = {:rating => 'PG', :secure => false}.merge(options)
66
77
  end
67
78
 
79
+ alias :has_gravatar :is_gravtastic
80
+
68
81
  #
69
82
  # Returns a symbol of the instance method where the Gravatar is pulled from.
70
83
  #
71
84
  # For example, if your users email is returned by the method <tt>#gobagaldy_gook</tt> then it
72
- # will return the symbol <tt>:'gobagaldy_gook'</tt>.
85
+ # will return the symbol <tt>:gobagaldy_gook</tt>.
73
86
  #
74
87
  def gravatar_source
75
88
  @gravatar_source
76
89
  end
77
-
90
+
78
91
  #
79
92
  # Returns a hash of all the default gravtastic options.
80
93
  #
81
94
  def gravatar_defaults
82
95
  @gravatar_defaults
83
96
  end
84
-
97
+
85
98
  #
86
99
  # Returns <tt>true</tt> if the gravatar_source is set. <tt>false</tt> if not. Easy!
87
100
  #
@@ -119,16 +132,16 @@ module Gravtastic
119
132
  def gravatar_url(options={})
120
133
  options = self.class.gravatar_defaults.merge(options)
121
134
 
122
- @gravatar_url = gravatar_url_base(options[:secure]) + gravatar_filename + parse_url_options_hash(options)
135
+ @gravatar_url = gravatar_hostname(options[:secure]) + gravatar_filename + parse_url_options_hash(options)
123
136
  end
124
137
 
125
138
  private
126
139
 
127
- GRAVTASTIC_VALID_PARAMS = [:size, :rating, :default]
128
-
140
+ #
141
+ # Parses a hash options for the image parameters. Returns a CGI escaped string.
142
+ #
129
143
  def parse_url_options_hash(options)
130
-
131
- options.delete_if { |key,_| ![:size,:rating,:default].include?(key) }
144
+ options.delete_if { |key,_| ![:size, :rating, :default].include?(key) }
132
145
 
133
146
  unless options.empty?
134
147
  '?' + options.map do |pair|
@@ -140,10 +153,16 @@ module Gravtastic
140
153
  end
141
154
  end
142
155
 
143
- def gravatar_url_base(secure)
156
+ #
157
+ # Returns the Gravatar.com hostname
158
+ #
159
+ def gravatar_hostname(secure)
144
160
  'http' + (secure ? 's://secure.' : '://') + 'gravatar.com/avatar/'
145
161
  end
146
-
162
+
163
+ #
164
+ # Returns the filename of the gravatar image.
165
+ #
147
166
  def gravatar_filename
148
167
  if gravatar_id
149
168
  gravatar_id + '.png'
@@ -151,8 +170,13 @@ module Gravtastic
151
170
  ''
152
171
  end
153
172
  end
154
-
173
+
155
174
  end
156
175
  end
157
176
 
158
- ActiveRecord::Base.send(:include, Gravtastic::Model) if defined?(ActiveRecord) # :nodoc:
177
+ ActiveRecord::Base.send(:include, Gravtastic::Resource) if defined?(ActiveRecord) # :nodoc:
178
+
179
+ if defined?(DataMapper) # :nodoc:
180
+ DataMapper::Resource.append_inclusions Gravtastic::Resource
181
+ DataMapper::Model.append_extensions Gravtastic::Resource::ClassMethods
182
+ end
@@ -0,0 +1,38 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'rubygems'
3
+
4
+ require 'activerecord'
5
+ require 'dm-core'
6
+
7
+ reload_gravtastic!
8
+
9
+ class User
10
+ include DataMapper::Resource
11
+ is_gravtastic
12
+ end
13
+
14
+ describe ActiveRecord::Base do
15
+
16
+ it "includes Gravtastic::Resource" do
17
+ ActiveRecord::Base.included_modules.should include(Gravtastic::Resource)
18
+ end
19
+
20
+ it "responds to .is_gravtastic" do
21
+ ActiveRecord::Base.should respond_to(:is_gravtastic)
22
+ end
23
+
24
+ end
25
+
26
+ describe DataMapper::Resource do
27
+
28
+ it "includes Gravtastic::Resource" do
29
+ User.included_modules.should include(Gravtastic::Resource)
30
+ end
31
+
32
+ it "responds to .is_gravtastic" do
33
+ User.should respond_to(:is_gravtastic)
34
+ end
35
+
36
+ end
37
+
38
+
@@ -1,10 +1,10 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
- describe "Gravtastic::Model" do
3
+ describe Gravtastic::Resource do
4
4
 
5
5
  before(:each) do
6
6
  @user = mock('User')
7
- @user.class.send(:include, Gravtastic::Model)
7
+ @user.class.send(:include, Gravtastic::Resource)
8
8
  @klass = @user.class
9
9
  end
10
10
 
@@ -34,47 +34,47 @@ describe "Gravtastic::Model" do
34
34
 
35
35
  end
36
36
 
37
- describe ".has_gravatar" do
37
+ describe ".is_gravtastic" do
38
38
 
39
39
  it "sets .gravatar_source to :email by default" do
40
- @klass.has_gravatar
40
+ @klass.is_gravtastic
41
41
  @klass.gravatar_source.should == :email
42
42
  end
43
43
 
44
44
  it "sets .gravatar_defaults to { :rating => 'PG', :secure => false } by default" do
45
- @klass.has_gravatar
45
+ @klass.is_gravtastic
46
46
  @klass.gravatar_defaults.should == { :rating => 'PG', :secure => false }
47
47
  end
48
48
 
49
- it "sets .gravatar_source to :email by default, even when :defaults are supplied" do
50
- @klass.has_gravatar :defaults => { :secure => true }
49
+ it "sets .gravatar_source to :email by default, even when defaults are supplied" do
50
+ @klass.is_gravtastic :secure => true
51
51
  @klass.gravatar_source.should == :email
52
52
  end
53
53
 
54
54
  it "keeps either :rating or :secure if only the other is passed as a default" do
55
- @klass.has_gravatar :defaults => { :secure => true }
55
+ @klass.is_gravtastic :secure => true
56
56
  @klass.gravatar_defaults.should == { :rating => 'PG', :secure => true }
57
57
  end
58
58
 
59
59
  it "changes .gravatar_source" do
60
60
  lambda {
61
- @klass.has_gravatar :on => :other_method
61
+ @klass.is_gravtastic :with => :other_method
62
62
  }.should change(@klass, :gravatar_source)
63
63
  end
64
64
 
65
65
  it "changes .gravatar_defaults" do
66
66
  lambda {
67
- @klass.has_gravatar :defaults => { :rating => 'R18' }
67
+ @klass.is_gravtastic :rating => 'R18'
68
68
  }.should change(@klass, :gravatar_defaults)
69
69
  end
70
70
 
71
71
  it "sets .gravatar_source to the value of :on" do
72
- @klass.has_gravatar :on => :other_method
72
+ @klass.is_gravtastic :with => :other_method
73
73
  @klass.gravatar_source.should == :other_method
74
74
  end
75
75
 
76
76
  it "sets .gravatar_defaults to the value of :defaults" do
77
- @klass.has_gravatar :defaults => { :rating => 'R18'}
77
+ @klass.is_gravtastic :rating => 'R18'
78
78
  @klass.gravatar_defaults.should == { :rating => 'R18', :secure => false }
79
79
  end
80
80
 
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,9 @@
1
1
  $TESTING=true
2
2
  $:.push File.join(File.dirname(__FILE__), '..', 'lib')
3
3
 
4
+ def reload_gravtastic!
5
+ Object.class_eval { remove_const :Gravtastic } if defined? Gravtastic
6
+ require File.join(File.dirname(__FILE__),'../lib/gravtastic')
7
+ end
8
+
4
9
  require 'gravtastic'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gravtastic
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Lloyd
@@ -27,6 +27,7 @@ files:
27
27
  - README.textile
28
28
  - Rakefile
29
29
  - lib/gravtastic.rb
30
+ - spec/gravtastic_integration_spec.rb
30
31
  - spec/gravtastic_spec.rb
31
32
  - spec/spec.opts
32
33
  - spec/spec_helper.rb