From 70034e10efc13b6381f0b6acb9a7abb203aca481 Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Tue, 25 Jan 2011 16:11:42 +0100 Subject: [PATCH] API method for updating the tags of a table --- app/controllers/api/json/tables_controller.rb | 2 +- app/models/table.rb | 5 ++++- spec/acceptance/api/tables_spec.rb | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/json/tables_controller.rb b/app/controllers/api/json/tables_controller.rb index 3d90f80358..1f877abc84 100644 --- a/app/controllers/api/json/tables_controller.rb +++ b/app/controllers/api/json/tables_controller.rb @@ -23,7 +23,7 @@ class Api::Json::TablesController < ApplicationController respond_to do |format| format.json do begin - @table.update_fields(params, [:name]) + @table.update_all(params) render :nothing => true, :status => 200 rescue render :json => { :errors => @table.errors.full_messages}.to_json, :status => 400 diff --git a/app/models/table.rb b/app/models/table.rb index 35ff53cf44..d9c643738f 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -6,8 +6,11 @@ class Table < Sequel::Model(:user_tables) PRIVATE = 0 PUBLIC = 1 + # Ignore mass-asigment on not allowed columns + self.strict_param_setting = false + # Allowed columns - set_allowed_columns(:name, :privacy) + set_allowed_columns(:name, :privacy, :tags) ## Callbacks def validate diff --git a/spec/acceptance/api/tables_spec.rb b/spec/acceptance/api/tables_spec.rb index d2f3d190d4..00866a89f8 100644 --- a/spec/acceptance/api/tables_spec.rb +++ b/spec/acceptance/api/tables_spec.rb @@ -80,4 +80,22 @@ feature "Tables JSON API" do table.name.should == "My brand new name" end + scenario "Update the tags of a table" do + user = create_user + table = create_table :user_id => user.id + + authenticate_api user + + put_json "/api/json/table/#{table.id}/update", {:tags => "tag1, tag2, tag3"} + response.status.should == 200 + Tag.count.should == 3 + tags = Tag.filter(:user_id => user.id, :table_id => table.id).all + tags.size.should == 3 + tags.map(&:name).sort.should == %W{ tag1 tag2 tag3 } + + put_json "/api/json/table/#{table.id}/update", {:tags => ""} + response.status.should == 200 + Tag.count.should == 0 + end + end