Skip to content

Commit

Permalink
Use Sequel refinement for pg_array_op
Browse files Browse the repository at this point in the history
  • Loading branch information
jirutka committed Jul 14, 2015
1 parent f2f931a commit 563c29c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
6 changes: 3 additions & 3 deletions app/interactors/api/compound_events.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'set'
require 'interpipe/interactor'
require 'corefines/object'
require 'sequel/extensions/core_refinements'

require 'models/course'
require 'models/person'
Expand All @@ -9,6 +10,7 @@ module Interactors
module Api
class CompoundEvents
using Corefines::Object::blank?
using Sequel::CoreRefinements

include Interpipe::Interactor

Expand Down Expand Up @@ -43,9 +45,7 @@ def courses
def teachers
# SELECT id, full_name FROM people
# WHERE id IN (SELECT unnest(teacher_ids) FROM events WHERE ...)
array_op = Sequel.pg_array(:teacher_ids)
teacher_ids = events.select(array_op.unnest)

teacher_ids = events.select(:teacher_ids.pg_array.unnest)
Person.where(id: teacher_ids).select(:id, :full_name)
end

Expand Down
7 changes: 4 additions & 3 deletions app/models/event.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'period'
require 'parallel'
require 'sequel/extensions/core_refinements'

class Event < Sequel::Model
using Sequel::CoreRefinements

many_to_one :room
many_to_one :course
Expand All @@ -11,9 +13,8 @@ class Event < Sequel::Model
alias :sequence_number :relative_sequence_number

def self.with_person(username)
teacher_op = Sequel.pg_array(:teacher_ids)
student_op = Sequel.pg_array(:student_ids)
filter(teacher_op.contains([username])).or(student_op.contains([username]))
filter(:teacher_ids.pg_array.contains([username]))
.or(:student_ids.pg_array.contains([username]))
end

def period
Expand Down
7 changes: 4 additions & 3 deletions app/models/parallel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
require 'models/timetable_slot'
require 'sirius/event_factory'
require 'sirius/teaching_time'
require 'sequel/extensions/core_refinements'

class Parallel < Sequel::Model
using Sequel::CoreRefinements

many_to_one :course
one_to_many :timetable_slots

def self.for_teacher(teacher_id)
array_op = Sequel.pg_array(:teacher_ids)
filter(array_op.contains([teacher_id]))
# alternative: filter(teacher_id => array_op.any)
filter(:teacher_ids.pg_array.contains([teacher_id]))
# alternative: filter(teacher_id => :teacher_ids.pg_array.any)
end

def to_s
Expand Down
5 changes: 3 additions & 2 deletions app/models/person.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
require 'models/parallel'
require 'sequel/extensions/core_refinements'

class Person < Sequel::Model
using Sequel::CoreRefinements

# FIXME: this should be put somewhere else so we don't have dependency on other model
# see also ApiHelper#user_allowed?
def self.teacher?(username)
teacher_ids = Sequel.pg_array(:teacher_ids)
DB.select(1)
.where(Parallel.where(teacher_ids.contains([username])).exists)
.where(Parallel.where(:teacher_ids.pg_array.contains([username])).exists)
.any?
end
end
5 changes: 4 additions & 1 deletion config/initializers/database.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'sequel'
# Must be required before Sequel extensions are loaded.
require 'sequel/extensions/core_refinements'

# XXX: postgresql plugin passes the 'postgresql:' URL,
# while Sequel expects 'postgres' adapter
require 'sequel'
db_url = Config.database_url.sub(/\Apostgresql:/, 'postgres:')

DB = Sequel.connect(db_url, max_connections: Config.db_pool)
Expand Down

0 comments on commit 563c29c

Please sign in to comment.