Skip to content

Commit

Permalink
Add support for touch
Browse files Browse the repository at this point in the history
  • Loading branch information
macario committed Oct 1, 2012
1 parent de6d48c commit f921eff
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -8,5 +8,7 @@ db

/doc
/.idea
*.swo
*.swp

test/connections/databases.yml
31 changes: 30 additions & 1 deletion lib/composite_primary_keys/persistence.rb
Expand Up @@ -35,6 +35,35 @@ def destroy
freeze
end

def touch(name = nil)
attributes = timestamp_attributes_for_update_in_model
attributes << name if name

unless attributes.empty?
current_time = current_time_from_proper_timezone
changes = {}

attributes.each do |column|
column = column.to_s
changes[column] = write_attribute(column, current_time)
end

changes[self.class.locking_column] = increment_lock if locking_enabled?

puts changes.inspect

@changed_attributes.except!(*changes.keys)

relation = self.class.send(:relation)
arel_table = self.class.arel_table
primary_key = self.class.primary_key

primary_key_predicate = relation.cpk_id_predicate(arel_table, Array(primary_key), Array(id))

self.class.unscoped.where(primary_key_predicate).update_all(changes) == 1
end
end

def update(attribute_names = @attributes.keys)
klass = self.class
if !self.composite?
Expand All @@ -56,4 +85,4 @@ def update(attribute_names = @attributes.keys)
klass.connection.update stmt.to_sql
end
end
end
end
8 changes: 6 additions & 2 deletions test/fixtures/db_definitions/postgresql.sql
Expand Up @@ -18,14 +18,18 @@ create table reference_codes (
create table products (
id serial not null,
name varchar(50) default null,
primary key (id)
primary key (id),
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);

create table tariffs (
tariff_id int not null,
start_date date not null,
amount int default null,
primary key (tariff_id, start_date)
primary key (tariff_id, start_date),
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);

create table product_tariffs (
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/db_definitions/sqlite.sql
Expand Up @@ -16,13 +16,17 @@ create table reference_codes (

create table products (
id int(11) not null primary key,
name varchar(50) default null
name varchar(50) default null,
created_at TIMESTAMP,
updated_at TIMESTAMP
);

create table tariffs (
tariff_id int(11) not null,
start_date date not null,
amount integer(11) default null,
created_at TIMESTAMP,
updated_at TIMESTAMP,
primary key (tariff_id, start_date)
);

Expand Down
23 changes: 23 additions & 0 deletions test/test_touch.rb
@@ -0,0 +1,23 @@
# Test cases devised by Santiago that broke the Composite Primary Keys
# code at one point in time. But no more!!!
require File.expand_path('../abstract_unit', __FILE__)

class TestTouch < ActiveSupport::TestCase
fixtures :products, :tariffs

def test_touching_a_record_updates_its_timestamp
tariff = tariffs(:flat)
previous_amount = tariff.amount
previously_updated_at = tariff.updated_at

tariff.amount = previous_amount + 1
tariff.touch

assert_not_equal previously_updated_at, tariff.updated_at
assert_equal previous_amount + 1, tariff.amount
assert tariff.amount_changed?, 'tarif amount should have changed'
assert tariff.changed?, 'tarif should be marked as changed'
tariff.reload
assert_not_equal previously_updated_at, tariff.updated_at
end
end

0 comments on commit f921eff

Please sign in to comment.