Skip to content

Commit

Permalink
Merge pull request #143 from anykeyh/v0.7.2
Browse files Browse the repository at this point in the history
migration to crystal 0.31
  • Loading branch information
anykeyh authored Oct 2, 2019
2 parents 63bbbae + 2e04a76 commit 08a8528
Show file tree
Hide file tree
Showing 43 changed files with 145 additions and 136 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
CRYSTAL_BIN ?= $(shell which crystal)

test:
$(CRYSTAL_BIN) spec -Dquiet
$(CRYSTAL_BIN) spec -Dquiet --warnings=all

6 changes: 3 additions & 3 deletions manual/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MyModel
column i : Clear::Interval
end
puts "Expected time: #{Time.now + MyModel.first!.i}"
puts "Expected time: #{Time.local + MyModel.first!.i}"
```

- Add `Clear::TimeInDay` columns type, which stands for the `time` object in PostgreSQL.
Expand All @@ -42,7 +42,7 @@ time = Clear::TimeInDay.parse("12:33")
puts time.hour # 12
puts time.minutes # 0
Time.now.at(time) # Today at 12:33:00
Time.local.at(time) # Today at 12:33:00
time.to_s # 12:33:00
time.to_s(false) # don't show seconds => 12:33
Expand Down Expand Up @@ -252,7 +252,7 @@ Basically a transition version, to support Crystal 0.27. Some of the features of
updating the documentation !
- Add range support for `Sql::Node#in?` method:
```crystal
last_week_users = User.query.where{ created_at.in?(7.day.ago..Time.now) }.count
last_week_users = User.query.where{ created_at.in?(7.day.ago..Time.local) }.count
```
- Refactoring of the nodes and clause of the SQL builder, avoiding array instantiation (usage of tuple instead) and starting to use Node as much as possible to build clauses.
- Add `Clear::Expression.unsafe` which does exactly what it says:
Expand Down
2 changes: 1 addition & 1 deletion manual/model/lifecycle/callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class User

after :create, :send_email

before(:update) { |m| m.as(User).updated_at = Time.now }
before(:update) { |m| m.as(User).updated_at = Time.local }

def send_email
EmailManager.send_email(subject: "welcome #{full_name} !", body: "...")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Filter the query – The Expression Engine

Because Collection represents SQL SELECT query, they offer way to filter the query.
Clear offer the Expression Engine, which is inspired by Sequel.
Because Collection represents SQL SELECT query, they offer way to filter the query.
Clear offer the Expression Engine, which is inspired by Sequel.
It basically helps you to write complex filter conditions without sacrificing on code expressiveness.

## The where clause
Expand Down Expand Up @@ -91,7 +91,7 @@ Expression engine manage natively range, array and other methods as see below.
Range:

```ruby
User.query.where{ created_at.in?(5.days.from_now .. Time.now) } # WHERE created_at > ... AND created_at < ...
User.query.where{ created_at.in?(5.days.from_now .. Time.local) } # WHERE created_at > ... AND created_at < ...
```

#### Array / Tuples:
Expand Down
4 changes: 2 additions & 2 deletions manual_old/model/Hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ before(:validate) do |model|
model = model.as(self)
unless model.persisted?
now = Time.now
now = Time.local
model.created_at = now
model.updated_at = now
end
Expand All @@ -41,7 +41,7 @@ after(:validate) do |model|
# In the case the updated_at has been changed, we do not override.
# It happens on first insert, in the before validation setup.
if model.changed? && !model.updated_at_column.changed?
model.updated_at = Time.now
model.updated_at = Time.local
end
end
```
Expand Down
6 changes: 3 additions & 3 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ shards:

ameba:
github: veelenga/ameba
commit: 8843235f5faeeb97b2a834a60825e05ca5798e86
commit: 8a27a36c49ee45954680931dc7ea457bd08aa2ec

db:
github: crystal-lang/crystal-db
version: 0.5.1
version: 0.7.0

generate:
github: anykeyh/generate.cr
Expand All @@ -22,5 +22,5 @@ shards:

pg:
github: will/crystal-pg
version: 0.17.0
commit: cafe7b4de4f6e0b0ef620c96de2b9d03f0157347

6 changes: 3 additions & 3 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: clear
version: 0.7
version: 0.7.2

authors:
- Yacine Petitprez <[email protected]>
Expand All @@ -18,7 +18,7 @@ dependencies:
branch: master
pg:
github: will/crystal-pg
version: "~> 0.17"
branch: master
inflector:
github: phoffer/inflector.cr
version: "~> 0.1.8"
Expand All @@ -30,6 +30,6 @@ development_dependencies:
github: veelenga/ameba
branch: master

crystal: 0.30
crystal: 0.31.1

license: MIT
8 changes: 4 additions & 4 deletions spec/extensions/bcrypt_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ module BCryptSpec
User.create!({encrypted_password: Crypto::Bcrypt::Password.create("abcd")})

User.query.count.should eq 1
User.query.first!.encrypted_password.should eq "abcd"
User.query.first!.encrypted_password.should_not eq "abce"
User.query.first!.encrypted_password.verify("abcd").should be_true
User.query.first!.encrypted_password.verify("abce").should be_false

usr = User.query.first!

usr.encrypted_password = Crypto::Bcrypt::Password.create("lorem.ipsum")
usr.save!

User.query.first!.encrypted_password.should_not eq "abcd"
User.query.first!.encrypted_password.should eq "lorem.ipsum"
User.query.first!.encrypted_password.verify("abcd").should be_false
User.query.first!.encrypted_password.verify("lorem.ipsum").should be_true
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/extensions/time_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ module IntervalSpec
# TimeSpan
[1.hour, 1.day, 1.month].each do |span|
i = Clear::Interval.new(span)
now = Time.now
now = Time.local

(now + i).to_unix.should eq( (now+span).to_unix)
(now - i).to_unix.should eq( (now-span).to_unix )
end

i = Clear::Interval.new(months: 1, days: -1, minutes: 12)
now = Time.now
now = Time.local

(now + i).to_unix.should eq( (now+1.month-1.day+12.minute).to_unix)
(now - i).to_unix.should eq( (now-1.month+1.day-12.minute).to_unix)
Expand Down
83 changes: 45 additions & 38 deletions spec/model/cache_spec.cr
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
require "../spec_helper"
require "./cache_schema"


module CacheSpec
describe "Clear::Model" do
temporary do
Clear::Migration::Manager.instance.reinit!
MigrateSpec10.new.apply(Clear::Migration::Direction::UP)
def self.reinit
Clear::Migration::Manager.instance.reinit!
MigrateSpec10.new.apply(Clear::Migration::Direction::UP)

User.create [{id: 101, name: "User 1"}]
User.create [{id: 102, name: "User 2"}]
User.create [{id: 101, name: "User 1"}]
User.create [{id: 102, name: "User 2"}]

Category.create [{id: 201, name: "Test"}]
Category.create [{id: 202, name: "Test 2"}]
Category.create [{id: 201, name: "Test"}]
Category.create [{id: 202, name: "Test 2"}]

Post.create [{id: 301, published: true, user_id: 101, category_id: 201, content: "Lorem ipsum"}]
Post.create [{id: 302, published: true, user_id: 102, category_id: 201, content: "Lorem ipsum"}]
Post.create [{id: 303, published: true, user_id: 102, category_id: 202, content: "Lorem ipsum"}]
Post.create [{id: 304, published: true, user_id: 101, category_id: 202, content: "Lorem ipsum"}]
Post.create [{id: 301, published: true, user_id: 101, category_id: 201, content: "Lorem ipsum"}]
Post.create [{id: 302, published: true, user_id: 102, category_id: 201, content: "Lorem ipsum"}]
Post.create [{id: 303, published: true, user_id: 102, category_id: 202, content: "Lorem ipsum"}]
Post.create [{id: 304, published: true, user_id: 101, category_id: 202, content: "Lorem ipsum"}]
end

context "cache system" do
it "manage has_many relations" do
describe "Clear::Model" do
context "cache system" do
it "manage has_many relations" do
temporary do
reinit
Clear::Model::QueryCache.reset_counter
# relations has_many
User.query.first!.posts.count.should eq(2)
Expand All @@ -29,41 +33,44 @@ module CacheSpec
Clear::Model::QueryCache.cache_hitted.should eq(1)
end
end
end

it "can be chained" do
temporary do
Clear::Model::QueryCache.reset_counter
User.query.with_posts(&.with_category).each do |user|
user.posts.each do |p|
case p.id
when 301, 302
(p.category.not_nil!.id == 201).should eq(true)
when 303, 304
(p.category.not_nil!.id == 202).should eq(true)
end
it "can be chained" do
temporary do
reinit
Clear::Model::QueryCache.reset_counter
User.query.with_posts(&.with_category).each do |user|
user.posts.each do |p|
case p.id
when 301, 302
(p.category.not_nil!.id == 201).should eq(true)
when 303, 304
(p.category.not_nil!.id == 202).should eq(true)
end
end
end
end
end

it "can be called in chain on the through relations" do
temporary do
# Relation belongs_to
Clear::Model::QueryCache.reset_counter
Post.query.with_user.each do |post|
post.user.not_nil!
end
Clear::Model::QueryCache.cache_hitted.should eq(4) # Number of posts
it "can be called in chain on the through relations" do
temporary do
reinit
# Relation belongs_to
Clear::Model::QueryCache.reset_counter
Post.query.with_user.each do |post|
post.user.not_nil!
end
Clear::Model::QueryCache.cache_hitted.should eq(4) # Number of posts

Category.query.with_users { |q| q.order_by("users.id", "asc") }.order_by("id", "asc").each do |c|
c.users.each do |user|
c.id.not_nil!
user.id.not_nil!
end
Category.query.with_users { |q| q.order_by("users.id", "asc") }.order_by("id", "asc").each do |c|
c.users.each do |user|
c.id.not_nil!
user.id.not_nil!
end
end
end
end

end
end
end
2 changes: 1 addition & 1 deletion spec/model/model_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ module ModelSpec
end

it "works on date fields with different timezone" do
now = Time.now
now = Time.local

temporary do
reinit
Expand Down
2 changes: 1 addition & 1 deletion spec/sql/select_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ module SelectSpec
end

it "can stack with `AND` operator" do
now = Time.now
now = Time.local
r = select_request.from(:users).where { users.id == nil }.where {
var("users", "updated_at") >= now
}
Expand Down
2 changes: 1 addition & 1 deletion src/clear/cli/generators/migration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Clear::CLI::Generator
if name
name_underscore = name.underscore
class_name = name.camelcase
migration_uid = Time.now.to_unix.to_s.rjust(10, '0')
migration_uid = Time.local.to_unix.to_s.rjust(10, '0')

g["migration_uid"] = migration_uid
g["class_name"] = class_name
Expand Down
2 changes: 1 addition & 1 deletion src/clear/cli/generators/model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Clear::CLI::Generator
class_name = name.camelcase

fields = @argv.join("|")
migration_uid = Time.now.to_unix.to_s.rjust(10, '0')
migration_uid = Time.local.to_unix.to_s.rjust(10, '0')

g["model_class"] = class_name
g["migration_uid"] = migration_uid
Expand Down
12 changes: 6 additions & 6 deletions src/clear/expression/expression.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
# Instead of writing:
#
# ```
# model_collection.where("created_at BETWEEN ? AND ?", 1.day.ago, DateTime.now)
# model_collection.where("created_at BETWEEN ? AND ?", 1.day.ago, DateTime.local)
# ```
#
# You can write:
# ```
# model_collection.where { created_at.between(1.day.ago, DateTime.now) }
# model_collection.where { created_at.between(1.day.ago, DateTime.local) }
# ```
#
# or even:
# ```
# model_collection.where { created_at.in?(1.day.ago..DateTime.now) }
# model_collection.where { created_at.in?(1.day.ago..DateTime.local) }
# ```
#
# (Note for the later, it will generate `created_at > 1.day.ago AND created_at < DateTime.now`)
# (Note for the later, it will generate `created_at > 1.day.ago AND created_at < DateTime.local`)
#
# ## Limitations
#
Expand Down Expand Up @@ -140,8 +140,8 @@ class Clear::Expression
#
# ## Example
# ```
# Clear::Expression[Time.now] # < "2017-04-03 23:04:43.234 +08:00"
# Clear::Expression[Time.now, date: true] # < "2017-04-03"
# Clear::Expression[Time.local] # < "2017-04-03 23:04:43.234 +08:00"
# Clear::Expression[Time.local, date: true] # < "2017-04-03"
# ```
def self.safe_literal(x : Time, date : Bool = false) : String
{"'", x.to_utc.to_s(date ? DATABASE_DATE_FORMAT : DATABASE_DATE_TIME_FORMAT), "'"}.join
Expand Down
2 changes: 1 addition & 1 deletion src/clear/expression/nodes/between.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Clear::Expression::Node::Between < Clear::Expression::Node
def initialize(@target : Node, @starts : BetweenType, @ends : BetweenType)
end

def resolve
def resolve : String
{"(",
@target.resolve,
" BETWEEN ",
Expand Down
2 changes: 1 addition & 1 deletion src/clear/expression/nodes/double_operator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require "./node"
class Clear::Expression::Node::DoubleOperator < Clear::Expression::Node
def initialize(@a : Node, @b : Node, @op : String); end

def resolve
def resolve : String
{"(", @a.resolve, " ", @op, " ", @b.resolve, ")"}.join
end
end
2 changes: 1 addition & 1 deletion src/clear/expression/nodes/function.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require "./node"
class Clear::Expression::Node::Function < Clear::Expression::Node
def initialize(@name : String, @args : Array(String)); end

def resolve
def resolve : String
{@name, "(", @args.join(", "), ")"}.join
end
end
2 changes: 1 addition & 1 deletion src/clear/expression/nodes/in_array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require "./node"
class Clear::Expression::Node::InArray < Clear::Expression::Node
def initialize(@target : Node, @array : Array(String)); end

def resolve
def resolve : String
if @array.size == 0
"FALSE" # If array is empty, return "FALSE" expression
else
Expand Down
2 changes: 1 addition & 1 deletion src/clear/expression/nodes/in_range.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require "./node"
class Clear::Expression::Node::InRange < Clear::Expression::Node
def initialize(@target : Node, @range : Range(String, String), @exclusive = false); end

def resolve
def resolve : String
rt = @target.resolve
final_op = @exclusive ? " < " : " <= "

Expand Down
Loading

0 comments on commit 08a8528

Please sign in to comment.