Skip to content

Commit

Permalink
Update crud.md docs with skip_timestamps examples
Browse files Browse the repository at this point in the history
  • Loading branch information
stufro committed Oct 1, 2023
1 parent b9a6940 commit 177c930
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
3 changes: 1 addition & 2 deletions docker/docker-compose.sqlite.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
version: '2'
services:
spec:
#command: 'bash -c "cd /app/user && bin/ameba && crystal tool format --check && sqlite3 --version && crystal spec"'
command: 'bash -c "cd /app/user && crystal spec"'
command: 'bash -c "cd /app/user && bin/ameba && crystal tool format --check && sqlite3 --version && crystal spec"'
extends:
file: ../docker-compose.yml
service: spec
Expand Down
22 changes: 22 additions & 0 deletions docs/crud.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ post.body = "Check this out."
post.save! # raises when save failed
```

To skip the validation callbacks, pass in `validate: false`:

```crystal
post.save(validate: false)
post.save!(validate: false)
```

You can also pass in `skip_timestamps` to save without changing the `updated_at` field on update:

```crystal
post.save(skip_timestamps: true)
post.save!(skip_timestamps: true)
```

## Read

### find
Expand Down Expand Up @@ -102,6 +116,14 @@ post = Post.find 1
post.update!(name: "Granite Really Rocks!") # Assigns attributes and calls save!. Will throw an exception when the save failed
```

To update a record without changing the `updated_at` field, you can pass in `skip_timestamps`:

```crystal
post = Post.find 1
post.update({name: "Granite Really Rocks!"}, skip_timestamps: true)
post.update!({name: "Granite Really Rocks!"}, skip_timestamps: true)
```

## Delete

Delete a specific record.
Expand Down
18 changes: 0 additions & 18 deletions spec/granite/transactions/create_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@ describe "#create" do
reserved_word.all.should eq("foo")
end
end

describe "when skip_timestamps is true" do
it "does not set the created_at and updated_at fields" do
parent = Parent.create({name: "New Parent"}, skip_timestamps: true)

parent.created_at.should eq nil
parent.updated_at.should eq nil
end
end
end

describe "#create!" do
Expand All @@ -58,13 +49,4 @@ describe "#create!" do
Parent.create!(name: "")
end
end

describe "when skip_timestamps is true" do
it "does not set the created_at and updated_at fields" do
parent = Parent.create!({name: "New Parent"}, skip_timestamps: true)

parent.created_at.should eq nil
parent.updated_at.should eq nil
end
end
end
16 changes: 8 additions & 8 deletions src/granite/transactions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ module Granite::Transactions
create(args.to_h)
end

def create(args, skip_timestamps : Bool = false)
def create(args)
instance = new
instance.set_attributes(args.to_h.transform_keys(&.to_s))
instance.save(skip_timestamps: skip_timestamps)
instance.set_attributes(args.transform_keys(&.to_s))
instance.save
instance
end

def create!(**args)
create!(args.to_h)
end

def create!(args, skip_timestamps : Bool = false)
instance = create(args, skip_timestamps: skip_timestamps)
def create!(args)
instance = create(args)

unless instance.errors.empty?
raise Granite::RecordNotSaved.new(self.name, instance)
Expand Down Expand Up @@ -113,14 +113,14 @@ module Granite::Transactions
{% end %}
end

private def __create(skip_timestamps : Bool = false)
private def __create
{% begin %}
{% primary_key = @type.instance_vars.find { |ivar| (ann = ivar.annotation(Granite::Column)) && ann[:primary] } %}
{% raise raise "A primary key must be defined for #{@type.name}." unless primary_key %}
{% raise "Composite primary keys are not yet supported for '#{@type.name}'." if @type.instance_vars.select { |ivar| ann = ivar.annotation(Granite::Column); ann && ann[:primary] }.size > 1 %}
{% ann = primary_key.annotation(Granite::Column) %}

set_timestamps unless skip_timestamps
set_timestamps
fields = self.class.content_fields.dup
params = content_values

Expand Down Expand Up @@ -214,7 +214,7 @@ module Granite::Transactions
__after_update
else
__before_create
__create(skip_timestamps: skip_timestamps)
__create
__after_create
end
__after_save
Expand Down

0 comments on commit 177c930

Please sign in to comment.