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

Api-Q-4 bulk delete finished #105

Open
wants to merge 7 commits into
base: feature/api
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
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,8 @@ gem 'bootstrap-sass', '~> 2.3'
gem 'crummy'
gem 'meta-tags', require: 'meta_tags'
gem 'sitemap_generator'

# i18n
gem "rails-i18n"

gem 'rack-cors', require: 'rack/cors'
8 changes: 7 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ GEM
pry (~> 0.9)
slop (~> 3.0)
rack (1.6.4)
rack-cors (0.4.0)
rack-protection (1.5.3)
rack
rack-test (0.6.3)
Expand All @@ -448,6 +449,9 @@ GEM
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
rails-i18n (4.0.6)
i18n (~> 0.7)
railties (~> 4.0)
railties (4.2.6)
actionpack (= 4.2.6)
activesupport (= 4.2.6)
Expand Down Expand Up @@ -664,7 +668,9 @@ DEPENDENCIES
pry
pry-rails
pry-remote
rack-cors
rails (= 4.2.6)
rails-i18n
ransack
redis
redis-objects
Expand Down Expand Up @@ -699,4 +705,4 @@ DEPENDENCIES
xray-rails

BUNDLED WITH
1.12.5
1.13.6
21 changes: 21 additions & 0 deletions app/controllers/api/base_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
module Api
class BaseController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :set_locale
before_action :enable_cors
def index
@message = "api root"
end

def set_locale
I18n.locale = extract_locale_from_accept_language_header
end

private

def extract_locale_from_accept_language_header
locale = request.env['HTTP_ACCEPT_LANGUAGE'].split(',').first rescue I18n.default_locale
end

def enable_cors
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE'
response.headers['Access-Control-Allow-Headers'] = 'Origin, X-Atmosphere-tracking-id, X-Atmosphere-Framework, X-Cache-Date, Content-Type, X-Atmosphere-Transport, X-Remote, api_key, auth_token, *'
response.headers['Access-Control-Request-Method'] = 'GET, POST, PUT, DELETE'
response.headers['Access-Control-Request-Headers'] = 'Origin, X-Atmosphere-tracking-id, X-Atmosphere-Framework, X-Cache-Date, Content-Type, X-Atmosphere-Transport, X-Remote, api_key, *'
end
end
end
36 changes: 36 additions & 0 deletions app/controllers/api/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,41 @@ class SitesController < ::Api::BaseController
def index
@sites = Site.all
end

def create
@site = Site.new(params_site)
if @site.save
render :json => @site.to_json, :status => 200
else
render :status => 400, :json => { :error => @site.errors.full_messages.join(",") }
end
end

def update
@site = Site.find(params[:id])
if @site.update(params_site)
render :json => @site, :status => 200
else
render :status => 400, :json => { :error => @site.errors.full_messages.join(",") }
end
end

def destroy
@site = Site.find(params[:id])
@site.destroy
render :json => { :message => "site deleted!"}, :status => 200
end

def bulk_delete
ids = Array(params[:sites_ids])
sites_to_be_deleted = ids.map{ |id| Site.find_by_id(id) }.compact
sites_to_be_deleted.each{ |site| site.destroy }
render :json => { :message => "sites deleted!"}, :status => 200
end

private
def params_site
params.require(:site).permit(:name, :host, :subdomain, :data)
end
end
end
9 changes: 8 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class Application < Rails::Application
g.factory_girl dir: "spec/factories"
end

config.middleware.insert_before 0, 'Rack::Cors' do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post, :delete, :put, :options]
end
end

# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
Expand All @@ -31,7 +38,7 @@ class Application < Rails::Application

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# config.i18n.default_locale = "zh-TW"

# disable after_commit & after_rollback of model callbacks
config.active_record.raise_in_transactional_callbacks = true
Expand Down
5 changes: 5 additions & 0 deletions config/locales/sites.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
zh-TW:
activerecord:
attributes:
site:
name: "站點名稱"
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

namespace :api do
root to: "base#index"
resources :sites
resources :sites do
collection do
post :bulk_delete
end
end
end

namespace :admin do
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
enable_extension "hstore"
enable_extension "postgis"
enable_extension "hstore"

create_table "authorizations", force: :cascade do |t|
t.integer "provider"
Expand Down
80 changes: 80 additions & 0 deletions spec/controllers/api/sites_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,84 @@
expect(response.status).to eq(200)
end
end

describe "#create" do

it "should return 200 when successfully created" do
post "/api/sites", :site => { :name => 'aaa', :host => 'bbb', :subdomain => 'ccc', :data => 'ddd' }
expect(response.status).to eq(200)
end

it "should return 400 when failed to create" do
post "/api/sites", :site => { :name => '' }
expect(response.status).to eq(400)
end

it "should return error message in json format when failed to create" do
post "/api/sites", :site => { :name => '' }
res = JSON.parse(response.body)
expect(res['error']).to eq("Name can't be blank")
end

end

describe "#update" do

before do
Site.create( :id => 1, :name => "aaa", :host => "bbb", :subdomain => "ccc", :data => "ddd")
end

it "should update site name" do
patch "/api/sites/1", :site => { :name => "updated" }
site = Site.find(1)
expect(site.name).to eq("updated")
end

it "should return 400 when failed to update" do
patch "/api/sites/1", :site => { :name => "" }
expect(response.status).to eq(400)
end

it "should return error message in json format when failed to update" do
patch "/api/sites/1", :site => { :name => '' }
res = JSON.parse(response.body)
expect(res['error']).to eq("Name can't be blank")
end

end

describe "#destroy" do

before do
Site.create( :id => 1, :name => "aaa", :host => "bbb", :subdomain => "ccc", :data => "ddd")
end

it "should return 200 when successfully deleted" do
delete "/api/sites/1"
expect(response.status).to eq(200)
end
end

describe "#I18n" do

it "should return error message in different languages according to the first locale in http accept language" do
post "/api/sites", { :site => { :name => '' } }, { 'Accept-Language' => 'zh-TW,en' }
res = JSON.parse(response.body)
expect(res['error']).to eq("站點名稱 不能是空白字元")
end

end

describe "#bulk delete" do

before { FactoryGirl.create_list(:site, 10) }

it "should delete all the given sites" do
sites_ids = Site.all.pluck('id').sample(8)
post "/api/sites/bulk_delete", :sites_ids => sites_ids
expect(Site.all.size).to eq(2)
end

end

end