Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for touch

  • Loading branch information...
commit f921eff0db7af9a62f368c898dae01fae1639242 1 parent de6d48c
Macario Ortega maca authored
2  .gitignore
View
@@ -8,5 +8,7 @@ db
/doc
/.idea
+*.swo
+*.swp
test/connections/databases.yml
31 lib/composite_primary_keys/persistence.rb
View
@@ -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?
@@ -56,4 +85,4 @@ def update(attribute_names = @attributes.keys)
klass.connection.update stmt.to_sql
end
end
-end
+end
8 test/fixtures/db_definitions/postgresql.sql
View
@@ -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 (
6 test/fixtures/db_definitions/sqlite.sql
View
@@ -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)
);
23 test/test_touch.rb
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.