Skip to content

Commit

Permalink
Merge pull request #12 from magoosh/persistence-methods
Browse files Browse the repository at this point in the history
New persistence methods
  • Loading branch information
zmillman committed Mar 19, 2015
2 parents 7739043 + bf66de7 commit 930c848
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
32 changes: 14 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,19 @@ end
Attribute methods are inferred from the associated SQLite table definition.

```ruby
message = Message.new(subject: "Welcome!", body: "If you have any questions...")
# => #<Message: @id=nil @subject="Welcome!" @body="If you have any..." ...>
message.satisfaction
# => 0.0
message = Message.new(subject: "Welcome!")
# => #<Message: @id=nil @subject="Welcome!" @body=nil, @created_at=nil ...>
```

Manage persistence with `save!`, `delete!`, and `persisted?`
Manage persistence with `create`, `save`, `destroy`, and `persisted?`

```ruby
message = Message.new(subject: "Welcome!", body: "If you have any questions...")
message.save!
message.id
# => 1
message.delete!
message = Message.create(subject: "Welcome!")
message.body = "If you have any questions, just ask us :)"
message.save
# SQL: UPDATE messages SET subject = ?, body = ?, ... WHERE id = ?
# Params: ["Welcome!", "If you have any questions, just ask :)", ..., 1]
message.destroy
message.persisted?
# => false
```
Expand Down Expand Up @@ -177,7 +176,7 @@ class Message < MotionRecord::Base
serialize :read_at, :time
end

Message.create!(subject: "Hello!", read_at: Time.now)
Message.create(subject: "Hello!", read_at: Time.now)
# SQL: INSERT INTO messages (subject, body, read_at, ...) VALUES (?, ?, ?...)
# Params: ["Hello!", nil, 1420099200, ...]
Message.first.read_at
Expand All @@ -200,11 +199,10 @@ class Survey < MotionRecord::Base
serialize :response, :json
end

survey = Survey.new(response: {nps: 10, what_can_we_improve: "Nothing :)"})
survey.save!
survey = Survey.create(response: {nps: 10, what_can_we_improve: "Nothing :)"})
# SQL: INSERT INTO surveys (response) VALUES (?)
# Params: ['{"nps":10, "what_can_we_improve":"Nothing :)"}']
Survey.first
survey
# => #<Survey: @id=1 @response={"nps"=>10, "what_can_we_improve"=>"Nothing :)"}>
```

Expand All @@ -216,12 +214,10 @@ class User < MotionRecord::Base
serialize :birthday, :date
end

drake = User.new(birthday: Time.new(1986, 10, 24))
drake.save!
drake = User.create(birthday: Time.new(1986, 10, 24))
# SQL: INSERT INTO users (birthday) VALUES (?)
# Params: ["1986-10-24"]
User.first.birthday
# => 1986-10-24 00:00:00 UTC
# => #<User: @id=1, @birthday=1986-10-24 00:00:00 UTC>
```

#### Custom Serializers
Expand Down
34 changes: 25 additions & 9 deletions lib/motion_record/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ module Persistence

TIMESTAMP_COLUMNS = [:created_at, :updated_at]

def save!
def save
persist!
end

def delete!
if persisted?
self.class.where(primary_key_condition).delete_all
else
raise "Can't delete unpersisted records"
end
def destroy
delete!
end

def persisted?
Expand Down Expand Up @@ -41,11 +37,24 @@ def persist!
self.class.where(primary_key_condition).update_all(table_params)
else
connection.insert self.class.table_name, table_params
if self.class.primary_key
# HACK: This assumes that primary keys are monotonically increasing
newest_primary_key = self.class.maximum(self.class.primary_key)
self.self.instance_variable_set "@#{self.class.primary_key}", newest_primary_key
end
end

self.mark_persisted!
end

def delete!
if persisted?
self.class.where(primary_key_condition).delete_all
else
raise "Can't delete unpersisted records"
end
end

# Update persistence auto-timestamp attributes
def apply_persistence_timestamps
self.updated_at = Time.now if self.class.attribute_names.include?(:updated_at)
Expand All @@ -57,8 +66,15 @@ def primary_key_condition
end

module ClassMethods
def create!(attributes={})
self.new(attributes).save!
# Create a new record
#
# attributes - a Hash of attribute values
#
# Returns the created record
def create(attributes={})
record = self.new(attributes)
record.save
record
end

# Sybmol name of the primary key column
Expand Down
4 changes: 4 additions & 0 deletions lib/motion_record/schema/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def table_exists?
connection.table_exists?(table_name)
end

def primary_key
nil
end

def create_table
table = Schema::TableDefinition.new(table_name, id: false)
table.integer :version, :null => false
Expand Down
2 changes: 1 addition & 1 deletion lib/motion_record/schema/migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def run
pending_migrations.each do |migration|
migration.execute
@migrated_versions << migration.version
Schema::Migration.create!(version: migration.version)
Schema::Migration.create(version: migration.version)
end
end

Expand Down

0 comments on commit 930c848

Please sign in to comment.