Skip to content

Commit

Permalink
Replace incompatible babe_squeel
Browse files Browse the repository at this point in the history
Baby Squeel is incompatible with Rails >5.2.1 when using polymorphic
associations. This commit removes baby_squeel and replaces all usages
with plain where statements and some new scopes.

Most squeel clauses are replaces with AR where clauses with a little bit
SQL as string. Values are passed as placeholders. The more complex feed
logic is extracted into three scopes on the Feed model.
  • Loading branch information
jgraichen committed Sep 12, 2019
1 parent f1c87ee commit f2065dd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 28 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ gem 'will_paginate'
gem 'paginate-responder', '~> 2.0'
gem 'decorate-responder', '~> 2.0'
gem 'api-responder'
gem 'baby_squeel'

gem 'pg', '~> 1.0' # missing compability with rails

Expand Down
9 changes: 0 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ GEM
ast (2.4.0)
autoprefixer-rails (9.6.1.1)
execjs
baby_squeel (1.3.1)
activerecord (>= 4.2.0)
join_dependency (~> 0.1.2)
polyamorous (~> 1.3)
bcrypt (3.1.13)
bourbon (6.0.0)
thor (~> 0.19)
Expand Down Expand Up @@ -147,8 +143,6 @@ GEM
i18n (1.6.0)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.3)
join_dependency (0.1.4)
activerecord (>= 4.2.0)
jquery-rails (4.3.5)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
Expand Down Expand Up @@ -227,8 +221,6 @@ GEM
capybara (>= 2.1, < 4)
cliver (~> 0.3.1)
websocket-driver (>= 0.2.0)
polyamorous (1.3.3)
activerecord (>= 3.0)
powerpack (0.1.2)
pry (0.12.2)
coderay (~> 1.1.0)
Expand Down Expand Up @@ -399,7 +391,6 @@ DEPENDENCIES
accept_values_for (>= 0.7.4)
api-responder
autoprefixer-rails (~> 9.6)
baby_squeel
bcrypt
bourbon (~> 6.0)
brakeman
Expand Down
19 changes: 16 additions & 3 deletions app/models/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,21 @@ class Feed < ApplicationRecord
belongs_to :source
has_many :fetches, class_name: 'FeedFetch'
has_many :messages, as: :messageable

scope :fetch_needed, -> {
where.has { next_fetch_at < Time.zone.now }.order(:next_fetch_at)
where('next_fetch_at < ?', Time.zone.now).order(:next_fetch_at)
}

scope :archived, lambda {
joins(source: :canteen).where(sources: {canteens: {state: 'archived' }})
}

scope :inactive, lambda {
where('(schedule IS NULL OR id IN (?))', Feed.archived.select(:id))
}

scope :active, lambda {
where('(schedule IS NOT NULL AND id NOT IN (?))', Feed.archived.select(:id))
}

def canteen
Expand All @@ -14,8 +27,8 @@ def canteen

def feed_timespans
{
lastday: fetches.where.has { executed_at > 1.day.ago },
lastweek: fetches.where.has { executed_at > 1.week.ago },
lastday: fetches.where('executed_at > ?', 1.day.ago),
lastweek: fetches.where('executed_at > ?', 1.week.ago),
total: fetches
}
end
Expand Down
21 changes: 6 additions & 15 deletions lib/open_mensa/update_feeds_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,13 @@ def do
private

def fix_next_fetch_ats
disabled_feeds = Feed
.joins(source: :canteen)
.where.has { source.canteen.state == 'archived' }
.select(:id)
changes = Feed.inactive.where.not(next_fetch_at: nil).update_all(next_fetch_at: nil)

changes = Feed
.where.has { ((schedule == nil) | id.in(disabled_feeds)) & (next_fetch_at != nil) }
.update_all(next_fetch_at: nil)

Feed
.where.has { (schedule != nil) & id.not_in(disabled_feeds) & (next_fetch_at == nil) }
.each do |feed|
feed.next_fetch_at = process_schedule(feed, :last)
feed.save!
changes += 1
end
Feed.active.where(next_fetch_at: nil).each do |feed|
feed.next_fetch_at = process_schedule(feed, :last)
feed.save!
changes += 1
end

changes
end
Expand Down

0 comments on commit f2065dd

Please sign in to comment.