diff --git a/lib/noosfero/api/helpers.rb b/lib/noosfero/api/helpers.rb index 8dce8a4..5f2a020 100644 --- a/lib/noosfero/api/helpers.rb +++ b/lib/noosfero/api/helpers.rb @@ -243,6 +243,16 @@ require_relative '../../find_by_contents' end end + def by_period(scope, params, attribute) + from_param = "from_#{attribute}".to_sym + until_param = "until_#{attribute}".to_sym + from_date = DateTime.parse(params.delete(from_param)) if params[from_param] + until_date = DateTime.parse(params.delete(until_param)) if params[until_param] + scope = scope.where("#{attribute} >= ?", from_date) unless from_date.nil? + scope = scope.where("#{attribute} <= ?", until_date) unless until_date.nil? + scope + end + def select_filtered_collection_of(object, method, params) conditions = make_conditions_with_parameter(params) order = make_order_with_parameters(object,method,params) @@ -251,6 +261,7 @@ require_relative '../../find_by_contents' objects = object.send(method) objects = by_reference(objects, params) objects = by_categories(objects, params) + [:start_date, :end_date].each { |attribute| objects = by_period(objects, params, attribute) } objects = objects.where(conditions).where(timestamp).reorder(order) diff --git a/test/api/articles_test.rb b/test/api/articles_test.rb index a7947e6..f27ff0a 100644 --- a/test/api/articles_test.rb +++ b/test/api/articles_test.rb @@ -744,4 +744,24 @@ class ArticlesTest < ActiveSupport::TestCase assert_not_includes json['article']['children'].map {|a| a['id']}, child.id end + should 'list events with period for start date' do + Article.destroy_all + article1 = fast_create(Event, profile_id: user.person.id, start_date: Time.now) + article2 = fast_create(Event, profile_id: user.person.id, start_date: Time.now + 2.day) + params[:until_start_date] = Time.now + 1.day + get "/api/v1/articles/?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal json["articles"].map { |a| a["id"] }, [article1.id] + end + + should 'list events with period for end date' do + Article.destroy_all + article1 = fast_create(Event, profile_id: user.person.id, end_date: Time.now) + article2 = fast_create(Event, profile_id: user.person.id, end_date: Time.now + 2.day) + params[:from_end_date] = Time.now + 1.day + get "/api/v1/articles/?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal json["articles"].map { |a| a["id"] }, [article2.id] + end + end -- libgit2 0.21.2