Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 4 compatibility #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ matrix:
- { rvm: 1.8.7, gemfile: gemfiles/activerecord_3_2.gemfile, env: DB=mysql }
- { rvm: 1.9.2, gemfile: gemfiles/activerecord_3_2.gemfile, env: DB=mysql }
- { rvm: 2.0.0, gemfile: gemfiles/activerecord_3_2.gemfile, env: DB=mysql }
- { rvm: 2.1.1, gemfile: gemfiles/activerecord_3_2.gemfile, env: DB=mysql }

# different activerecord versions
- { rvm: 1.8.7, gemfile: gemfiles/activerecord_2_2.gemfile, env: DB=mysql }
- { rvm: 1.9.3, gemfile: gemfiles/activerecord_2_3.gemfile, env: DB=mysql }
- { rvm: 1.9.3, gemfile: gemfiles/activerecord_3_0.gemfile, env: DB=mysql }
- { rvm: 1.9.3, gemfile: gemfiles/activerecord_3_1.gemfile, env: DB=mysql }
- { rvm: 1.9.3, gemfile: gemfiles/activerecord_4_0.gemfile, env: DB=mysql }
- { rvm: 2.1.1, gemfile: gemfiles/activerecord_4_1.gemfile, env: DB=mysql }

before_script:
- sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'create database if not exists oauth2_test;'; fi"
Expand Down
6 changes: 5 additions & 1 deletion Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ if RUBY_VERSION >= '1.9'
appraise 'activerecord_4_0' do
gem 'activerecord', '~> 4.0.0'
gem 'mysql', '~> 2.9.0' if ENV['DB'] == 'mysql'
gem 'protected_attributes', '~> 1.0.0'
end

appraise 'activerecord_4_1' do
gem 'activerecord', '~> 4.1.0'
gem 'mysql', '~> 2.9.0' if ENV['DB'] == 'mysql'
end
end
2 changes: 2 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ example in a Sinatra app:
end

post '/oauth/apps' do
# Note: The following assumes protected_attributes. For Rails 4 and above,
# use: @client = Songkick::OAuth2::Model::Client.new(params.require(:client).permit(:name, :redirect_uri))
@client = Songkick::OAuth2::Model::Client.new(params)
@client.save ? erb(:show_client) : erb(:new_client)
end
Expand Down
3 changes: 1 addition & 2 deletions gemfiles/activerecord_4_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ source "https://rubygems.org"

gem "activerecord", "~> 4.0.0"
gem "mysql", "~> 2.9.0"
gem "protected_attributes", "~> 1.0.0"

gemspec :path=>"../"
gemspec :path => "../"
8 changes: 8 additions & 0 deletions gemfiles/activerecord_4_1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "activerecord", "~> 4.1.0"
gem "mysql", "~> 2.9.0"

gemspec :path => "../"
2 changes: 1 addition & 1 deletion lib/songkick/oauth2/model/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Authorization < ActiveRecord::Base
validates_uniqueness_of :refresh_token_hash, :scope => :client_id, :allow_nil => true
validates_uniqueness_of :access_token_hash, :allow_nil => true

attr_accessible nil
attr_accessible nil if (defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR <= 3) || defined?(ProtectedAttributes)

class << self
private :create, :new
Expand Down
2 changes: 1 addition & 1 deletion lib/songkick/oauth2/model/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Client < ActiveRecord::Base
validates_presence_of :name, :redirect_uri
validate :check_format_of_redirect_uri

attr_accessible :name, :redirect_uri
attr_accessible :name, :redirect_uri if (defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR <= 3) || defined?(ProtectedAttributes)

before_create :generate_credentials

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.up
add_index :oauth2_authorizations, [:client_id, :code]
add_index :oauth2_authorizations, [:access_token_hash]
add_index :oauth2_authorizations, [:client_id, :access_token_hash]
add_index :oauth2_authorizations, [:client_id, :refresh_token_hash]
add_index :oauth2_authorizations, [:client_id, :refresh_token_hash], :name => 'index_oauth2_authorizations_client_id_refresh_token_hash' # Name must be <= 62 chars
end

def self.down
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ class SongkickOauth2SchemaAddUniqueIndexes < ActiveRecord::Migration

def self.up
FIELDS.each do |field|
remove_index :oauth2_authorizations, [:client_id, field]
add_index :oauth2_authorizations, [:client_id, field], :unique => true
if field == :refresh_token_hash
# This field needs a custom index name to keep the name <= 62 characters
remove_index :oauth2_authorizations, :name => "index_oauth2_authorizations_client_id_#{field}"
add_index :oauth2_authorizations, [:client_id, field], :unique => true, :name => "index_oauth2_authorizations_client_id_#{field}_u"
else
remove_index :oauth2_authorizations, [:client_id, field]
add_index :oauth2_authorizations, [:client_id, field], :unique => true
end
end
remove_index :oauth2_authorizations, [:access_token_hash]
add_index :oauth2_authorizations, [:access_token_hash], :unique => true
Expand Down
18 changes: 10 additions & 8 deletions spec/songkick/oauth2/model/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@
@client.should_not be_valid
end

it "cannot mass-assign client_id" do
@client.update_attributes(:client_id => 'foo')
@client.client_id.should_not == 'foo'
end

it "cannot mass-assign client_secret" do
@client.update_attributes(:client_secret => 'foo')
@client.client_secret.should_not == 'foo'
if (defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR <= 3) || defined?(ProtectedAttributes)
it "cannot mass-assign client_id" do
@client.update_attributes(:client_id => 'foo')
@client.client_id.should_not == 'foo'
end

it "cannot mass-assign client_secret" do
@client.update_attributes(:client_secret => 'foo')
@client.client_secret.should_not == 'foo'
end
end

it "has client_id and client_secret filled in" do
Expand Down
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require 'bundler/setup'

require 'active_record'
require 'protected_attributes' if defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR > 3

require 'songkick/oauth2/provider'

Expand Down