Permalink
Browse files

Add since param to posts_feed

  • Loading branch information...
1 parent 5ed03c1 commit a60f7d4bd8f85ac8bb04f5f3bde5784c45cd57f7 @jvatic jvatic committed May 28, 2013
Showing with 65 additions and 16 deletions.
  1. +31 −4 lib/tentd/feed.rb
  2. +34 −12 lib/tentd/feed/query.rb
View
@@ -19,18 +19,45 @@ def params
def query
q = Query.new(Model::Post)
- q.sort_columns = case params['sort_by']
+ # TODO: handle sort columns/order better
+ sort_columns = case params['sort_by']
when 'published_at'
- ['published_at DESC', 'version_published_at DESC']
+ ["#{q.table_name}.published_at DESC", "#{q.table_name}.version_published_at DESC"]
when 'version.published_at'
- 'version_published_at DESC'
+ ["#{q.table_name}.version_published_at DESC"]
else
- ['received_at DESC', 'version_received_at DESC']
+ ["#{q.table_name}.received_at DESC"]
end
+ q.sort_columns = sort_columns
q.query_conditions << "#{q.table_name}.user_id = ?"
q.query_bindings << env['current_user'].id
+ if params['since']
+ since_timestamp, since_version = params['since'].split(' ')
+ since_timestamp = since_timestamp.to_i
+
+ timestamp_column = q.sort_columns.split(' ').first
+
+ q.reverse_sort = true
+
+ if since_version
+ q.query_conditions << ["OR",
+ ["AND", "#{timestamp_column} >= ?", "#{q.table_name}.version > ?"],
+ "#{timestamp_column} > ?"
+ ]
+ q.query_bindings << since_timestamp
+ q.query_bindings << since_version
+ q.query_bindings << since_timestamp
+
+ sort_columns << "#{q.table_name}.version DESC"
+ q.sort_columns = sort_columns
+ else
+ q.query_conditions << "#{timestamp_column} > ?"
+ q.query_bindings << since_timestamp
+ end
+ end
+
if params['types']
requested_types = params['types'].uniq.map { |uri| TentType.new(uri) }
View
@@ -3,7 +3,7 @@ class Feed
class Query
attr_reader :model, :table_name, :select_columns, :joins, :sort_columns, :query_conditions, :query_bindings
- attr_accessor :limit
+ attr_accessor :limit, :reverse_sort
def initialize(model)
@model = model
@table_name = model.table_name
@@ -13,6 +13,7 @@ def initialize(model)
@select_columns = '*'
@joins = []
@sort_columns = nil
+ @reverse_sort = false
end
def select_columns=(columns)
@@ -35,18 +36,27 @@ def build_query_conditions(options = {})
sep = options[:conditions_sep] || 'AND'
query_conditions.map do |c|
- if c.kind_of?(Array) && ['OR', 'AND'].include?(c.first)
- c = c.dup
- c_sep = c.shift
- if c.size > 1
- "(#{c.join(" #{c_sep} ")})"
+ _build_conditions(c)
+ end.join(" #{sep} ")
+ end
+
+ def _build_conditions(conditions)
+ conditions = conditions.dup
+ if conditions.kind_of?(Array) && ['OR', 'AND'].include?(conditions.first)
+ sep = conditions.shift
+ if conditions.size > 1
+ conditions.map! {|c| c.kind_of?(Array) ? _build_conditions(c) : c }
+ "(#{conditions.join(" #{sep} ")})"
+ else
+ if conditions.kind_of?(Array)
+ _build_conditions(conditions)
else
- c.first
+ conditions.first
end
- else
- c
end
- end.join(" #{sep} ")
+ else
+ conditions
+ end
end
def to_sql(options = {})
@@ -56,14 +66,26 @@ def to_sql(options = {})
q << "WHERE #{build_query_conditions(options)}"
end
- q << "ORDER BY #{sort_columns}" if sort_columns
+ if sort_columns
+ if reverse_sort
+ q << "ORDER BY #{sort_columns.gsub("DESC", "ASC")}"
+ else
+ q << "ORDER BY #{sort_columns}"
+ end
+ end
+
q << "LIMIT #{limit.to_i}" if limit
q.join(' ')
end
def all(options = {})
- model.with_sql(to_sql(options), *query_bindings)
+ models = model.with_sql(to_sql(options), *query_bindings).to_a
+ if reverse_sort
+ models.reverse
+ else
+ models
+ end
end
end

0 comments on commit a60f7d4

Please sign in to comment.