Skip to content

Commit

Permalink
Permalink support
Browse files Browse the repository at this point in the history
  • Loading branch information
atd committed Oct 22, 2010
1 parent 1e18a3b commit bb84b3f
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -12,6 +12,7 @@ gem 'atd-ancestry', :require => 'ancestry'
gem 'devise'
gem 'inherited_resources'
gem 'cancan'
gem 'stringex'

gem "rspec-rails", ">= 2.0.0.beta"
gem "factory_girl"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -112,6 +112,7 @@ GEM
json_pure
rubyzip
sqlite3-ruby (1.3.1)
stringex (1.2.0)
thor (0.14.3)
treetop (1.4.8)
polyglot (>= 0.3.1)
Expand All @@ -133,3 +134,4 @@ DEPENDENCIES
rspec-rails (>= 2.0.0.beta)
ruby-debug (>= 0.10.3)
sqlite3-ruby
stringex
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -33,6 +33,7 @@ spec = Gem::Specification.new do |s|
s.add_dependency('atd-ancestry', '~> 1.3.0')
s.add_dependency('devise', '~> 1.1.3')
s.add_dependency('inherited_resources', '~> 1.1.2')
s.add_dependency('stringex', '~> 1.2.0')
end

Rake::GemPackageTask.new(spec) do |pkg|
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/groups_controller.rb
@@ -1,2 +1,9 @@
class GroupsController < InheritedResources::Base
protected

# Overwrite resource method to support permalink
# See InheritedResources::BaseHelpers#resource
def resource
@group ||= end_of_association_chain.find_by_permalink!(params[:id])
end
end
2 changes: 2 additions & 0 deletions app/models/actor.rb
Expand Up @@ -2,6 +2,8 @@
class Actor < ActiveRecord::Base
include SocialStream::Models::Supertype

acts_as_url :name, :url_attribute => :permalink

has_many :sent_ties,
:class_name => "Tie",
:foreign_key => 'sender_id',
Expand Down
15 changes: 14 additions & 1 deletion lib/social_stream/models/actor.rb
Expand Up @@ -13,7 +13,7 @@ module Actor

delegate :name, :name=,
:email, :email=,
:permalink, :permalink=,
:permalink,
:ties, :sent_ties, :received_ties,
:active_ties_to,
:sender_subjects, :receiver_subjects, :suggestion,
Expand All @@ -32,6 +32,10 @@ def actor!
actor || build_actor
end

def to_param
permalink
end

private

def initialize_reflexive_ties
Expand All @@ -56,6 +60,15 @@ def receiving_subject_classes
map(&:receiver_type).
map(&:constantize)
end

def find_by_permalink(perm)
joins(:actor).where('actors.permalink' => perm).first
end

def find_by_permalink!(perm)
find_by_permalink(perm) ||
raise(ActiveRecord::RecordNotFound)
end
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/social_stream/rails.rb
@@ -1,6 +1,10 @@
# Load Devise constant
# Permalinks:
require 'stringex'
# Hierchical relationships in Relation and Activity:
require 'ancestry'
# User authentication:
require 'devise'
# REST controllers:
require 'inherited_resources'

require 'social_stream/rails/common'
Expand Down
3 changes: 3 additions & 0 deletions spec/factories/actor.rb
@@ -0,0 +1,3 @@
Factory.define :actor do |s|
s.sequence(:name) { |n| "Actor #{ n }" }
end
16 changes: 16 additions & 0 deletions spec/models/actor_spec.rb
@@ -0,0 +1,16 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Actor do
it "should generate permalink" do
assert Factory(:actor).permalink.present?
end

it "should generate different permalink" do
a = Factory(:actor)
b = Factory(:actor, :name => a.name)

a.name.should == b.name
a.permalink.should_not == b.permalink
end

end
10 changes: 10 additions & 0 deletions spec/models/user_space.rb
@@ -0,0 +1,10 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe User do
it "should find by permalink" do
user = Factory(:user)

assert user.should == User.find_by_permalink(user.permalink)
end
end

0 comments on commit bb84b3f

Please sign in to comment.