Skip to content

Commit

Permalink
Merge pull request #50 from vast/rails4-support
Browse files Browse the repository at this point in the history
Rails4 support which plays nice even with Rails 3
  • Loading branch information
fredwu committed Oct 11, 2013
2 parents 14abab2 + 7fa8702 commit 751ec78
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ language: ruby
rvm:
- 1.9.3
- 2.0.0
gemfile:
- gemfiles/Gemfile.rails-3.2.x
- Gemfile
2 changes: 1 addition & 1 deletion api_taster.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Gem::Specification.new do |s|
s.files = `git ls-files`.split($\)
s.test_files = Dir["spec/**/*"]

s.add_dependency 'rails', '~> 3.2.0'
s.add_dependency 'rails', '>= 3.2'
s.add_dependency 'jquery-rails'
s.add_dependency 'sass-rails'
s.add_dependency 'bootstrap-sass', '~> 2.1'
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api_taster/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ApiTaster
class ApplicationController < ActionController::Base
layout proc { |controller| controller.request.xhr? ? nil : 'api_taster/application' }
layout proc { |controller| controller.request.xhr? ? false : 'api_taster/application' }

before_filter :reload_routes

Expand Down
5 changes: 5 additions & 0 deletions gemfiles/Gemfile.rails-3.2.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source "https://rubygems.org"

gemspec :path => '..'

gem "rails", "~> 3.2.6"
9 changes: 8 additions & 1 deletion lib/api_taster/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def normalise_routes!
route_set.routes.each do |route|
next if route.app.is_a?(Sprockets::Environment)
next if route.app == ApiTaster::Engine
next if route.defaults[:controller].to_s.starts_with?('rails/')

rack_app = discover_rack_app(route.app)

Expand Down Expand Up @@ -129,13 +130,19 @@ def split_input(input, route)
url_param_keys = route[:path].scan /:\w+/

url_params = input.reject { |k, v| ! ":#{k}".in?(url_param_keys) }
post_params = input.diff(url_params)
post_params = hash_diff(input, url_params)

{
:url_params => url_params,
:post_params => post_params
}
end

def hash_diff(h1, h2)
h1.dup.delete_if do |k, v|
h2[k] == v
end.merge!(h2.dup.delete_if { |k, v| h1.has_key?(k) })
end
end
end
end
20 changes: 20 additions & 0 deletions spec/controllers/api_taster/routes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,25 @@ module ApiTaster

assigns(:obsolete_definitions).should be_kind_of(Array)
end

context 'layout' do
context 'when request is not XHR' do
it 'renders application layout' do
get :index, :use_route => :api_taster

response.should render_template('api_taster/application')
end
end

context 'when request is XHR' do
before { request.stub(:xhr?) { true } }

it 'does not render layout' do
get :index, :use_route => :api_taster

response.should_not render_template('api_taster/application')
end
end
end
end
end
5 changes: 2 additions & 3 deletions spec/dummy/config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false

# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true

# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
Expand All @@ -34,4 +31,6 @@

# Expands the lines which load the assets
config.assets.debug = true

config.eager_load = false
end
2 changes: 2 additions & 0 deletions spec/dummy/config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
# config.active_record.auto_explain_threshold_in_seconds = 0.5

config.eager_load = true
end
5 changes: 2 additions & 3 deletions spec/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"

# Log error messages when you accidentally call methods on nil
config.whiny_nils = true

# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
Expand All @@ -34,4 +31,6 @@

# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr

config.eager_load = false
end
6 changes: 5 additions & 1 deletion spec/mapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ module ApiTaster
before(:all) do
Rails.application.routes.draw do
resources :dummy_users do
member { map_method :patch, :update }
if Rails.version.starts_with? '4'
member { map_method :patch, [:update] }
else
member { map_method :patch, :update }
end
end
end

Expand Down
3 changes: 3 additions & 0 deletions spec/route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ module ApiTaster
end
mount Rails.application => '/app'
mount proc {} => '/rack_app'

get 'rails/info/properties' => 'rails/info#properties', :as => :rails_info_properties
get '/' => 'rails/welcome#index'
end

Rails.application.stub(:routes).and_return(routes)
Expand Down

0 comments on commit 751ec78

Please sign in to comment.