Commit 0b16e860a96b899bb5d517f1469440633d1090c1
Exists in
staging
and in
1 other branch
Merge branch 'api-articles-period' into staging
Showing
2 changed files
with
31 additions
and
0 deletions
Show diff stats
lib/noosfero/api/helpers.rb
... | ... | @@ -267,6 +267,16 @@ require_relative '../../find_by_contents' |
267 | 267 | end |
268 | 268 | end |
269 | 269 | |
270 | + def by_period(scope, params, attribute) | |
271 | + from_param = "from_#{attribute}".to_sym | |
272 | + until_param = "until_#{attribute}".to_sym | |
273 | + from_date = DateTime.parse(params.delete(from_param)) if params[from_param] | |
274 | + until_date = DateTime.parse(params.delete(until_param)) if params[until_param] | |
275 | + scope = scope.where("#{attribute} >= ? or #{attribute} IS NULL", from_date) unless from_date.nil? | |
276 | + scope = scope.where("#{attribute} <= ? or #{attribute} IS NULL", until_date) unless until_date.nil? | |
277 | + scope | |
278 | + end | |
279 | + | |
270 | 280 | def select_filtered_collection_of(object, method, params) |
271 | 281 | conditions = make_conditions_with_parameter(params) |
272 | 282 | order = make_order_with_parameters(object,method,params) |
... | ... | @@ -275,6 +285,7 @@ require_relative '../../find_by_contents' |
275 | 285 | objects = object.send(method) |
276 | 286 | objects = by_reference(objects, params) |
277 | 287 | objects = by_categories(objects, params) |
288 | + [:start_date, :end_date].each { |attribute| objects = by_period(objects, params, attribute) } | |
278 | 289 | |
279 | 290 | objects = objects.where(conditions).where(timestamp).reorder(order) |
280 | 291 | ... | ... |
test/api/articles_test.rb
... | ... | @@ -748,4 +748,24 @@ class ArticlesTest < ActiveSupport::TestCase |
748 | 748 | assert_not_includes json['article']['children'].map {|a| a['id']}, child.id |
749 | 749 | end |
750 | 750 | |
751 | + should 'list events with period for start date' do | |
752 | + Article.destroy_all | |
753 | + article1 = fast_create(Event, profile_id: user.person.id, start_date: Time.now) | |
754 | + article2 = fast_create(Event, profile_id: user.person.id, start_date: Time.now + 2.day) | |
755 | + params[:until_start_date] = Time.now + 1.day | |
756 | + get "/api/v1/articles/?#{params.to_query}" | |
757 | + json = JSON.parse(last_response.body) | |
758 | + assert_equal json["articles"].map { |a| a["id"] }, [article1.id] | |
759 | + end | |
760 | + | |
761 | + should 'list events with period for end date' do | |
762 | + Article.destroy_all | |
763 | + article1 = fast_create(Event, profile_id: user.person.id, end_date: Time.now) | |
764 | + article2 = fast_create(Event, profile_id: user.person.id, end_date: Time.now + 2.day) | |
765 | + params[:from_end_date] = Time.now + 1.day | |
766 | + get "/api/v1/articles/?#{params.to_query}" | |
767 | + json = JSON.parse(last_response.body) | |
768 | + assert_equal json["articles"].map { |a| a["id"] }, [article2.id] | |
769 | + end | |
770 | + | |
751 | 771 | end | ... | ... |