Skip to content

Commit

Permalink
Merge pull request #327 from subvertallchris/rails4-cache_key
Browse files Browse the repository at this point in the history
adds cache_key support for rails
  • Loading branch information
andreasronge committed Apr 5, 2014
2 parents 2523f76 + 0dd0824 commit fe500cd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/neo4j/rails/attributes.rb
Expand Up @@ -126,6 +126,12 @@ def to_param
persisted? ? neo_id.to_s : nil
end

def cache_key
return "#{self.class.model_name.cache_key}/new" if new_record?
return "#{self.class.model_name.cache_key}/#{id}-#{self.updated_at.utc.to_s(:number)}" if self.respond_to?(:updated_at) && !self.updated_at.blank?
"#{self.class.model_name.cache_key}/#{id}"
end

def to_model
self
end
Expand Down
7 changes: 7 additions & 0 deletions spec/fixture/ice_cream.rb
Expand Up @@ -8,3 +8,10 @@ class IceCream < Neo4j::Rails::Model
validates_presence_of :flavour
end

class IceCreamStamp < Neo4j::Rails::Model
property :flavour, :index => :exact
property :created_at, type: DateTime
property :updated_at, type: DateTime
has_n(:ingredients).to(Ingredient)
validates_presence_of :flavour
end
27 changes: 26 additions & 1 deletion spec/integration/model_spec.rb
Expand Up @@ -169,7 +169,6 @@ class ::Property < Neo4j::Rails::Model
end
end


describe "error" do
it "the validation method 'errors' returns the validation errors" do
p = IceCream.new
Expand All @@ -181,6 +180,32 @@ class ::Property < Neo4j::Rails::Model
end
end

describe "cache_key method" do
describe "unpersisted object" do
it "should respond with plural_model/new" do
model = IceCreamStamp.new
model.cache_key.should eq 'ice_cream_stamps/new'
end
end

describe "persisted object" do
it "should respond with a valid cache key" do
model = IceCreamStamp.create(flavour: "vanilla")
#model.flavour = "chocolate" and model.save
model.cache_key.should eq "ice_cream_stamps/#{model.id}-#{model.updated_at.utc.to_s(:number)}"
end

context "when changed" do
it "should change cache_key value" do
model = IceCreamStamp.create(flavour: "vanilla")
start = model.cache_key and sleep 1
model.flavour = "chocolate" and model.save
model.cache_key.should_not eq start
end
end
end
end

describe "ActiveModel::Dirty" do

it "implements attribute_changed?, _change, _changed, _was, _changed? methods" do
Expand Down

0 comments on commit fe500cd

Please sign in to comment.