From 266abc1f984dbb944897a3272e742a2cd12b53d6 Mon Sep 17 00:00:00 2001 From: Fernando Blat Date: Mon, 24 Jan 2011 16:21:48 +0100 Subject: [PATCH] By default tables are public --- app/models/table.rb | 10 +++++++++- spec/acceptance/api/tables_spec.rb | 2 +- spec/models/table_spec.rb | 8 ++++---- spec/support/factories/tables.rb | 5 ++++- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/models/table.rb b/app/models/table.rb index e81cd7bb9b..5df659e3e2 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -8,6 +8,10 @@ class Table < Sequel::Model(:user_tables) ## Callbacks + def before_validation + self.privacy ||= PUBLIC + end + # Before creating a user table a table should be created in the database. # This table has an empty schema def before_create @@ -32,7 +36,11 @@ class Table < Sequel::Model(:user_tables) ## End of Callbacks def private? - privacy.nil? || privacy == PRIVATE + privacy == PRIVATE + end + + def public? + !private? end def toggle_privacy! diff --git a/spec/acceptance/api/tables_spec.rb b/spec/acceptance/api/tables_spec.rb index 0101c54429..4132ae7ed3 100644 --- a/spec/acceptance/api/tables_spec.rb +++ b/spec/acceptance/api/tables_spec.rb @@ -34,7 +34,7 @@ feature "Tables JSON API" do Capybara.current_driver = :rack_test user = create_user - table = create_table :user_id => user.id + table = create_table :user_id => user.id, :privacy => Table::PRIVATE table.should be_private diff --git a/spec/models/table_spec.rb b/spec/models/table_spec.rb index 83a3b0f1ba..dfac70cfd2 100644 --- a/spec/models/table_spec.rb +++ b/spec/models/table_spec.rb @@ -4,9 +4,9 @@ require 'spec_helper' describe Table do it "should have a privacy associated and it should be private by default" do - table = Table.new - table.privacy.should be_nil - table.should be_private + table = create_table + table.privacy.should_not be_nil + table.should_not be_private end it "should be associated to a database table" do @@ -67,7 +67,7 @@ describe Table do it "has a toggle_privacy! method to toggle the table privacy" do user = create_user - table = create_table :user_id => user.id + table = create_table :user_id => user.id, :privacy => Table::PRIVATE table.should be_private table.toggle_privacy! diff --git a/spec/support/factories/tables.rb b/spec/support/factories/tables.rb index 2cd18636c9..e0a5ee7fd1 100644 --- a/spec/support/factories/tables.rb +++ b/spec/support/factories/tables.rb @@ -3,7 +3,10 @@ module CartoDB def new_table(attributes = {}) attributes = attributes.dup attributes[:name] ||= String.random(10) - attributes[:user_id] ||= rand(1000) + if attributes[:user_id].nil? + user = create_user + attributes[:user_id] = user.id + end Table.new(attributes) end