Commit 6556aba36b11f35cd8b8cb6d941bcfc08cc807f8
1 parent
8c14cce3
Exists in
staging
and in
2 other branches
api: filter articles by period
Showing
2 changed files
with
31 additions
and
0 deletions
Show diff stats
lib/noosfero/api/helpers.rb
| @@ -243,6 +243,16 @@ require_relative '../../find_by_contents' | @@ -243,6 +243,16 @@ require_relative '../../find_by_contents' | ||
| 243 | end | 243 | end |
| 244 | end | 244 | end |
| 245 | 245 | ||
| 246 | + def by_period(scope, params, attribute) | ||
| 247 | + from_param = "from_#{attribute}".to_sym | ||
| 248 | + until_param = "until_#{attribute}".to_sym | ||
| 249 | + from_date = DateTime.parse(params.delete(from_param)) if params[from_param] | ||
| 250 | + until_date = DateTime.parse(params.delete(until_param)) if params[until_param] | ||
| 251 | + scope = scope.where("#{attribute} >= ?", from_date) unless from_date.nil? | ||
| 252 | + scope = scope.where("#{attribute} <= ?", until_date) unless until_date.nil? | ||
| 253 | + scope | ||
| 254 | + end | ||
| 255 | + | ||
| 246 | def select_filtered_collection_of(object, method, params) | 256 | def select_filtered_collection_of(object, method, params) |
| 247 | conditions = make_conditions_with_parameter(params) | 257 | conditions = make_conditions_with_parameter(params) |
| 248 | order = make_order_with_parameters(object,method,params) | 258 | order = make_order_with_parameters(object,method,params) |
| @@ -251,6 +261,7 @@ require_relative '../../find_by_contents' | @@ -251,6 +261,7 @@ require_relative '../../find_by_contents' | ||
| 251 | objects = object.send(method) | 261 | objects = object.send(method) |
| 252 | objects = by_reference(objects, params) | 262 | objects = by_reference(objects, params) |
| 253 | objects = by_categories(objects, params) | 263 | objects = by_categories(objects, params) |
| 264 | + [:start_date, :end_date].each { |attribute| objects = by_period(objects, params, attribute) } | ||
| 254 | 265 | ||
| 255 | objects = objects.where(conditions).where(timestamp).reorder(order) | 266 | objects = objects.where(conditions).where(timestamp).reorder(order) |
| 256 | 267 |
test/api/articles_test.rb
| @@ -744,4 +744,24 @@ class ArticlesTest < ActiveSupport::TestCase | @@ -744,4 +744,24 @@ class ArticlesTest < ActiveSupport::TestCase | ||
| 744 | assert_not_includes json['article']['children'].map {|a| a['id']}, child.id | 744 | assert_not_includes json['article']['children'].map {|a| a['id']}, child.id |
| 745 | end | 745 | end |
| 746 | 746 | ||
| 747 | + should 'list events with period for start date' do | ||
| 748 | + Article.destroy_all | ||
| 749 | + article1 = fast_create(Event, profile_id: user.person.id, start_date: Time.now) | ||
| 750 | + article2 = fast_create(Event, profile_id: user.person.id, start_date: Time.now + 2.day) | ||
| 751 | + params[:until_start_date] = Time.now + 1.day | ||
| 752 | + get "/api/v1/articles/?#{params.to_query}" | ||
| 753 | + json = JSON.parse(last_response.body) | ||
| 754 | + assert_equal json["articles"].map { |a| a["id"] }, [article1.id] | ||
| 755 | + end | ||
| 756 | + | ||
| 757 | + should 'list events with period for end date' do | ||
| 758 | + Article.destroy_all | ||
| 759 | + article1 = fast_create(Event, profile_id: user.person.id, end_date: Time.now) | ||
| 760 | + article2 = fast_create(Event, profile_id: user.person.id, end_date: Time.now + 2.day) | ||
| 761 | + params[:from_end_date] = Time.now + 1.day | ||
| 762 | + get "/api/v1/articles/?#{params.to_query}" | ||
| 763 | + json = JSON.parse(last_response.body) | ||
| 764 | + assert_equal json["articles"].map { |a| a["id"] }, [article2.id] | ||
| 765 | + end | ||
| 766 | + | ||
| 747 | end | 767 | end |