Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Active < EZ::Where::Compositions::Base
- def prepare(klass)
- active == true if klass.column_names.include? 'active'
- visible == true if klass.column_names.include? 'visible'
- hidden == false if klass.column_names.include? 'hidden'
- deactivated_at == :null if klass.column_names.include? 'deactivated_at'
- end
- end
- class Published < EZ::Where::Compositions::Base
- def prepare(klass, now = Time.new)
- append Active.new(klass)
- date_cond = create_condition
- date_cond += c { publish_at == :null; unpublish_at == :null }
- date_cond |= c { publish_at <= now } + (c { unpublish_at > now } | c { unpublish_at == :null })
- append date_cond
- end
- end
- Article.composition(:active).to_sql
- => ["articles.active = ? AND articles.hidden = ?
- AND articles.deactivated_at IS NULL", true, false]
- Article.composition(:published, Time.new.beginning_of_day).to_sql
- =>["(articles.active = ? AND articles.hidden = ? AND
- articles.deactivated_at IS NULL) AND
- ((articles.publish_at IS NULL AND articles.unpublish_at IS NULL) OR
- ((articles.publish_at <= ?) AND ((articles.unpublish_at > ?) OR
- (articles.unpublish_at IS NULL))))", true, false,
- Time.new.beginning_of_day, Time.new.beginning_of_day]
Add Comment
Please, Sign In to add comment