Bugfix: when changing the table name attribute, it should change the name of the table in the database

1.0
Fernando Blat 14 years ago
parent 18cf6ff203
commit 6b38288838

@ -22,10 +22,19 @@ class Table < Sequel::Model(:user_tables)
def before_validation
self.privacy ||= PUBLIC
self.name ||= set_table_name
self.name = set_table_name if self.name.blank?
super
end
def name=(new_name)
unless new?
owner.in_database do |user_database|
user_database.rename_table name, new_name
end
end
self[:name] = new_name
end
# Before creating a user table a table should be created in the database.
# This table has an empty schema
def before_create
@ -204,8 +213,8 @@ class Table < Sequel::Model(:user_tables)
end
def set_table_name
return if user_id.nil?
base_name = "Untitle table"
return base_name if user_id.nil?
i = 1
while Table.filter(:user_id => user_id, :name => base_name).count != 0
i += 1

@ -34,15 +34,29 @@ describe Table do
user = create_user
table = create_table :name => 'Wadus table', :user_id => user.id
Rails::Sequel.connection.table_exists?(table.name.to_sym).should be_false
user.in_database do |user_database|
user_database.table_exists?(table.name.to_sym).should be_true
end
end
it "should fetch empty data from the database by SQL sentences" do
it "should rename a database table when the attribute name is modified" do
user = create_user
table = create_table :name => 'Wadus table', :user_id => user.id
Rails::Sequel.connection.table_exists?(table.name.to_sym).should be_false
user.in_database do |user_database|
user_database.table_exists?(table.name.to_sym).should be_true
end
table.name = 'Wadus table #2'
table.save
user.in_database do |user_database|
user_database.table_exists?('Wadus table'.to_sym).should be_false
user_database.table_exists?('Wadus table #2'.to_sym).should be_true
end
end
it "should fetch empty data from the database by SQL sentences" do
user = create_user
table = create_table :name => 'Wadus table', :user_id => user.id
rows = table.execute_sql "select * from \"#{table.name}\" limit 1"
rows.should be_empty
table.rows_counted.should == 0

Loading…
Cancel
Save