Skip to content

Commit 4e4c52c

Browse files
committed
version 0.2.0
1 parent f78638c commit 4e4c52c

3 files changed

Lines changed: 29 additions & 17 deletions

File tree

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,43 @@ An small helper to set an scoped id for the model, based on the query you set as
1818

1919
**If using Lucky**, require the shard in your src/shards.cr file after requiring Avram:
2020

21-
```
21+
```crystal
2222
# In src/shards.cr
2323
# Put this after `require "avram"`
2424
require "avram_scoped_id"
2525
```
2626

2727
**If not using Lucky**, require the shard after Avram:
2828

29-
```
29+
```crystal
3030
# In whichever file you require your shards
3131
# Put this after `require "avram"`
3232
require "avram_scoped_id"
3333
```
3434

3535
## Usage
36-
Create column `scoped_id` in your model, as `Int64` - if not it will fail.
36+
Create column `scoped_id` in your model, as `Int64`
37+
You can call it differently if you want, just change to your column in the set method inside the `before_save` below
38+
39+
```crystal
40+
class AddScopeIdToArticle::V20200510065019 < Avram::Migrator::Migration::V1
41+
def migrate
42+
alter table_for(Article) do
43+
add scoped_id : Int64, default: 0
44+
end
45+
end
46+
47+
def rollback
48+
# drop table_for(Thing)
49+
end
50+
end
51+
```
52+
53+
> Feel free to add index on the scoped_id for faster queries later on.
3754
3855
Then You add it to the operation where you want to save the new scoped id. Like `SaveArticle`.
3956

40-
```
57+
```crystal
4158
class SaveArticle < Article::SaveOperation
4259
before_save do
4360
AvramScopedId.set column: scoped_id,
@@ -46,8 +63,6 @@ class SaveArticle < Article::SaveOperation
4663
end
4764
```
4865

49-
**scoped_id** is hardcoded for now due to how avram is built. so for now you need to have scoped_id on column.
50-
5166
> Good thing to know is that variables in the query might fail due to they can be `Nil` - So use `needs` and set that in the query and if necessary with `.not_nil!`
5267
5368
## Development

spec/avram_scoped_id_spec.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe AvramScopedId do
3737
end
3838
end
3939

40-
private def scoped(scoped_id, query = UserQuery.new)
40+
private def scoped(scoped_id : Avram::Attribute(Int64 | Nil), query = UserQuery.new)
4141
AvramScopedId.set(scoped_id, query)
4242
end
4343

src/avram_scoped_id.cr

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ require "avram"
22

33
# Set an scoped id based on the query.
44
module AvramScopedId
5-
VERSION = "0.1.0"
5+
VERSION = "0.2.0"
66

77
extend self
88

@@ -11,16 +11,13 @@ module AvramScopedId
1111
set(column, query)
1212
end
1313

14-
def set(column : Avram::Attribute(Int64 | Nil),
15-
query : Avram::Queryable) : Nil
16-
return unless column.value.nil?
17-
id = get_next_scoped_id(query, column)
18-
column.value = id.to_i64
19-
end
14+
macro set(column, query)
15+
return unless {{column}}.value.nil?
2016

21-
private def get_next_scoped_id(query, column)
22-
scope = query.scoped_id.select_max
17+
scope = {{query.id}}.{{column.id}}.select_max
2318
current_max_scoped_id = scope || 0
24-
current_max_scoped_id + 1
19+
id = current_max_scoped_id + 1
20+
21+
{{column.id}}.value = id.to_i64
2522
end
2623
end

0 commit comments

Comments
 (0)