Skip to content

Commit

Permalink
adapt classes to use \create method with opts arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
aajjbb committed Aug 9, 2017
1 parent 9e6b4bb commit d4d0fa3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 26 deletions.
9 changes: 7 additions & 2 deletions flows/followings.moon
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FollowingsFlow extends Flow
super ...
assert_error @current_user, "must be logged in"

follow_object: (object, type) =>
follow_object: (object, type) =>

f = Followings\create {
source_user_id: @current_user.id
Expand All @@ -26,7 +26,12 @@ class FollowingsFlow extends Flow
Notifications\notify_for target_user, object,
type, @current_user

event = Events\create(@current_user, object, Events.event_types.subscription)
event = Events\create({
user: @current_user
object: object
event_type: Events.event_types.subscription
})

TimelineEvents\deliver(@current_user, event)

f
Expand Down
22 changes: 11 additions & 11 deletions models/events.moon
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ class Events extends Model
}}
}

@create: (user, object, event_type) =>
assert user, "missing event's user"
assert object, "missing event's object"
assert event_type, "missing event_type, events must have a type"

opts = {
:event_type
source_user_id: user.id
object_object_id: object.id
object_object_type: @@object_type_for_object object
@create: (opts={}) =>
assert opts.user, "missing event's user"
assert opts.object, "missing event's object"
assert opts.event_type, "missing event_type, events must have a type"

event_opts = {
event_type: opts.event_type
source_user_id: opts.user.id
object_object_id: opts.object.id
object_object_type: @@object_type_for_object opts.object
}

event = safe_insert @, opts
event = safe_insert @, event_opts

return event

Expand Down
30 changes: 22 additions & 8 deletions models/timeline_events.moon
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
db = require "lapis.db"
import Model from require "lapis.db.model"
import Events, Followings, Users from require "models"


class TimelineEvents extends Model
@primary_key: { "user_id", "event_id" }

@create: (user, event) =>
@create: (opts={}) =>
assert opts.user_id, "user id not specified"
assert opts.event_id, "event id not specified"

super {
user_id: user.id
event_id: event.id
user_id: opts.user_id
event_id: opts.event_id
}

@delete: (user, event) =>
db.delete @table_name!, { user_id: user.id, event_id: event.id }

@deliver: (user, event) =>
import Events, Followings, Users from require "models"

switch event.event_type
when Events.event_types.update
followers = Followings\select "where object_id = ?", event.object_object_id
followers = Followings\select "where object_id = ? and object_type = ?", event.object_object_id, event.object_object_type

for users in *followers
follower_user = Users\find users.source_user_id
@@create(follower_user, event)
@@create({
user_id: follower_user.id
event_id: event
})
else
@@create(user, event)
@@create({
user_id: user.id
event_id: event.id
})

if Events\model_for_object_type(event.object_object_type) == Users
@@create(Users\find(event.object_object_id), event)
@@create({
user_id: Users\find(event.object_object_id).id
event_id: event.id
})

@user_timeline: (user) =>
@@select "where user_id = ?", user.id
30 changes: 25 additions & 5 deletions spec/models/events_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ describe "models.events", ->
user = factory.Users!
followed_user = factory.Users!

event = Events\create(user, followed_user, Events.event_types.subscription)
event = Events\create({
user: user
object: followed_user
event_type: Events.event_types.subscription
})

user_timeline = user\timeline!

assert.same user.id, event.source_user_id
Expand All @@ -28,7 +33,12 @@ describe "models.events", ->
user = factory.Users!
module = factory.Modules!

event = Events\create(user, module, Events.event_types.subscription)
event = Events\create({
user: user
object: module
event_type: Events.event_types.subscription
}
)
user_timeline = user\timeline!

assert.same user.id, event.source_user_id
Expand All @@ -41,7 +51,12 @@ describe "models.events", ->
user = factory.Users!
module = factory.Modules!

event = Events\create(user, module, Events.event_types.bookmark)
event = Events\create({
user: user
object: module
event_type: Events.event_types.bookmark
})

user_timeline = user\timeline!

assert.same user.id, event.source_user_id
Expand All @@ -54,10 +69,15 @@ describe "models.events", ->
user = factory.Users!
module = factory.Modules!

event = Events\create(user, module, Events.event_types.bookmark)
event = Events\create({
user: user
object: module
event_type: Events.event_types.bookmark
})

event_id = event.id

event\delete!

assert.same nil, Events\find user.id, event_id
assert.same Events\find(event_id), nil
assert.same 0, #user\timeline!

0 comments on commit d4d0fa3

Please sign in to comment.