Skip to content
This repository has been archived by the owner on Feb 5, 2019. It is now read-only.

Commit

Permalink
Ruby 1.8 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Morgan committed Feb 24, 2012
1 parent 47e27af commit edfa99b
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 121 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ gem 'stringex'
group :development do
gem 'jeweler'
gem 'yard'
gem 'RedCloth', require: 'redcloth'
gem 'RedCloth', :require => 'redcloth'
gem 'sqlite3'
gem 'rspec'
end
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ GEM
sprockets (2.0.0)
hike (~> 1.2)
rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.4)
stringex (1.3.0)
thor (0.14.6)
Expand Down
10 changes: 5 additions & 5 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ For any model you want to slug, include the @Slugalicious@ module and call
<pre><code>
class User < ActiveRecord::Base
include Slugalicious
slugged ->(user) { "#{user.first_name} #{user.last_name}" }
slugged lambda { |user| "#{user.first_name} #{user.last_name}" }
end
</code></pre>

Expand All @@ -83,7 +83,7 @@ but then add in the first name if two people share a last name, we'd call
@slugged@ like so:

<pre><code>
slugged :last_name, ->(user) { "#{user.first_name} #{user.last_name}" }
slugged :last_name, lambda { |user| "#{user.first_name} #{user.last_name}" }
</code></pre>

In the event that none of these generators manages to make a unique slug, a
Expand Down Expand Up @@ -111,7 +111,7 @@ symbol) or a @Proc@ that limits the scope of the uniqueness requirement:
class Product < ActiveRecord::Base
include Slugalicious
belongs_to :department
slugged :name, scope: :department_url_component
slugged :name, :scope => :department_url_component

private

Expand All @@ -134,7 +134,7 @@ included. You would usually use this method in conjunction with route globbing.
For example, we could set up our @routes.rb@ file like so:

<pre><code>
get '/products/*path', 'products#show', as: :products
get '/products/*path', 'products#show', :as => :products
</code></pre>

Then, in our @ProductsController@, we load the product from the path slug like
Expand Down Expand Up @@ -179,7 +179,7 @@ people know the URL has changed. As an example of how do this, we alter the
def find_product
@product = Product.find_from_slug_path(params[:path])
unless @product.active_slug?(params[:path].split('/').last)
redirect_to product_url(@product), status: :moved_permanently
redirect_to product_url(@product), :status => :moved_permanently
return false
end
return true
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ YARD::Rake::YardocTask.new('doc') do |doc|
doc.files = [ 'lib/**/*', 'README.textile', 'templates/slug.rb' ]
end

task(default: :spec)
task(:default => :spec)
28 changes: 14 additions & 14 deletions lib/slugalicious.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Slugalicious

included do
alias_method :to_param, :slug_with_path
has_many :slugs, as: :sluggable
has_many :slugs, :as => :sluggable
end

# Methods added to the class when this module is included.
Expand Down Expand Up @@ -125,11 +125,11 @@ def slugged(*slug_procs)
class_attribute :_slug_procs, :_slug_blacklist
class_attribute :_slugifier, :_slug_id_separator, :_slug_scope

self._slug_procs = slug_procs.map { |slug_proc| slug_proc.kind_of?(Symbol) ? ->(obj) { obj.send(slug_proc) } : slug_proc }
self._slugifier = options[:slugifier] || ->(string) { string.to_url }
self._slug_procs = slug_procs.map { |slug_proc| slug_proc.kind_of?(Symbol) ? lambda { |obj| obj.send(slug_proc) } : slug_proc }
self._slugifier = options[:slugifier] || lambda { |string| string.to_url }
self._slug_id_separator = options[:id_separator] || ';'
self._slug_scope = if options[:scope].kind_of?(Symbol) then
->(record) { record.send(options[:scope]).to_s }
lambda { |record| record.send(options[:scope]).to_s }
elsif options[:scope].kind_of?(Proc) then
options[:scope]
elsif options[:scope] then
Expand Down Expand Up @@ -174,7 +174,7 @@ def active_slug?(slug)
slug = if slugs.loaded? then
slugs.detect { |s| s.slug.downcase == slug.downcase }
else
slugs.where(slug: slug).first
slugs.where(:slug => slug).first
end
if slug then
slug.active?
Expand Down Expand Up @@ -210,17 +210,17 @@ def make_slug
valid_slugs_in_use = potential_slugs & slugs_in_use
unless valid_slugs_in_use.empty?
Slug.transaction do
slugs.update_all(active: false)
slugs.where(slug: valid_slugs_in_use.first).update_all(active: true)
slugs.update_all(:active => false)
slugs.where(:slug => valid_slugs_in_use.first).update_all(:active => true)
end
return
end

Slug.transaction do
# grab a list of all the slugs we can't use
scope = Slug.select(:slug).where(sluggable_type: self.class.to_s, slug: potential_slugs)
scope = Slug.select(:slug).where(:sluggable_type => self.class.to_s, :slug => potential_slugs)
if self.class._slug_scope then
scope = scope.where(scope: self.class._slug_scope[self])
scope = scope.where(:scope => self.class._slug_scope[self])
end
taken_slug_objects = scope.all

Expand All @@ -229,11 +229,11 @@ def make_slug
# no slugs available? nothing much else we can do
raise "Couldn't find a slug for #{self.inspect}; tried #{potential_slugs.join(', ')}" if available_slugs.empty?

slugs.update_all(active: false)
Slug.create!(sluggable: self,
slug: available_slugs.first,
active: true,
scope: self.class._slug_scope.try(:call, self))
slugs.update_all(:active => false)
Slug.create!(:sluggable => self,
:slug => available_slugs.first,
:active => true,
:scope => self.class._slug_scope.try(:call, self))
end

@active_slug = nil
Expand Down
28 changes: 14 additions & 14 deletions spec/slug_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,53 @@

context "[validation]" do
it "should not allow an active slug to be created if one already exists" do
Factory(:slug, sluggable: @record)
slug = Factory.build(:slug, sluggable: @record)
Factory(:slug, :sluggable => @record)
slug = Factory.build(:slug, :sluggable => @record)
slug.should_not be_valid
slug.errors[:active].should_not be_empty
end

it "should not allow a slug to be made active if one already exists" do
Factory(:slug, sluggable: @record)
slug = Factory(:slug, sluggable: @record, active: false)
Factory(:slug, :sluggable => @record)
slug = Factory(:slug, :sluggable => @record, :active => false)
slug.active = true
slug.should_not be_valid
slug.errors[:active].should_not be_empty
end

it "should allow an active slug to be created if none exists" do
slug = Factory.build(:slug, sluggable: @record)
slug = Factory.build(:slug, :sluggable => @record)
slug.should be_valid
end

it "should allow a slug to be made active if none exists" do
slug = Factory(:slug, sluggable: @record, active: false)
slug = Factory(:slug, :sluggable => @record, :active => false)
slug.active = true
slug.should be_valid
end

it "should allow an inactive slug to be modified if the active field is not changing" do
Factory(:slug, sluggable: @record)
slug = Factory(:slug, sluggable: @record, active: false)
Factory(:slug, :sluggable => @record)
slug = Factory(:slug, :sluggable => @record, :active => false)
slug.scope = 'test'
slug.should be_valid
end
end

describe "#activate!" do
it "should mark the slug as active" do
slug = Factory(:slug, sluggable: Factory(:user), active: false)
slug = Factory(:slug, :sluggable => Factory(:user), :active => false)
slug.activate!
slug.should be_active
end

it "should deactivate all the record's other slugs" do
record = Factory(:user)
s1 = Slug.for(record).first
s2 = Factory(:slug, sluggable: record, active: false)
s3 = Factory(:slug, sluggable: record, active: false)
s2 = Factory(:slug, :sluggable => record, :active => false)
s3 = Factory(:slug, :sluggable => record, :active => false)

slug = Factory(:slug, sluggable: record, active: false)
slug = Factory(:slug, :sluggable => record, :active => false)
slug.activate!

s1.reload.should_not be_active
Expand All @@ -65,7 +65,7 @@

context "[caching]" do
before :each do
Factory(:slug, sluggable: @record)
Factory(:slug, :sluggable => @record)
end

it "should write the slug to the cache" do
Expand All @@ -79,7 +79,7 @@
end

it "should remove the cached slug when the slug is changed" do
slug = Factory(:slug, sluggable: @record, active: false)
slug = Factory(:slug, :sluggable => @record, :active => false)
slug.activate!
RAILS_CACHE.read("Slug/User/#{@record.id}/slug").should be_nil
RAILS_CACHE.read("Slug/User/#{@record.id}/slug_with_path").should be_nil
Expand Down
Loading

0 comments on commit edfa99b

Please sign in to comment.