Guest User

Untitled

a guest
Feb 19th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. class Active < EZ::Where::Compositions::Base
  2. def prepare(klass)
  3. active == true if klass.column_names.include? 'active'
  4. visible == true if klass.column_names.include? 'visible'
  5. hidden == false if klass.column_names.include? 'hidden'
  6. deactivated_at == :null if klass.column_names.include? 'deactivated_at'
  7. end
  8. end
  9.  
  10. class Published < EZ::Where::Compositions::Base
  11. def prepare(klass, now = Time.new)
  12. append Active.new(klass)
  13. date_cond = create_condition
  14. date_cond += c { publish_at == :null; unpublish_at == :null }
  15. date_cond |= c { publish_at <= now } + (c { unpublish_at > now } | c { unpublish_at == :null })
  16. append date_cond
  17. end
  18. end
  19.  
  20.  
  21. Article.composition(:active).to_sql
  22.  
  23. => ["articles.active = ? AND articles.hidden = ?
  24. AND articles.deactivated_at IS NULL", true, false]
  25.  
  26.  
  27. Article.composition(:published, Time.new.beginning_of_day).to_sql
  28.  
  29. =>["(articles.active = ? AND articles.hidden = ? AND
  30. articles.deactivated_at IS NULL) AND
  31. ((articles.publish_at IS NULL AND articles.unpublish_at IS NULL) OR
  32. ((articles.publish_at <= ?) AND ((articles.unpublish_at > ?) OR
  33. (articles.unpublish_at IS NULL))))", true, false,
  34. Time.new.beginning_of_day, Time.new.beginning_of_day]
Add Comment
Please, Sign In to add comment