[Git][noosfero/noosfero][master] 2 commits: return articles descendents of events

Leandro Nunes gitlab at mg.gitlab.com
Fri Dec 7 11:45:56 BRST 2018


Leandro Nunes pushed to branch master at Noosfero / noosfero


Commits:
1d1307dd by Leandro Nunes dos Santos at 2018-12-07T13:01:09Z
return articles descendents of events

- - - - -
81f8cd1d by Leandro Nunes at 2018-12-07T13:45:53Z
Merge branch 'return-event-descendent-articles' into 'master'

return articles descendents of events

See merge request noosfero/noosfero!1652
- - - - -


2 changed files:

- app/api/helpers.rb
- test/api/articles_test.rb


Changes:

=====================================
app/api/helpers.rb
=====================================
@@ -341,7 +341,7 @@ module Api
       until_date = DateTime.parse(params.delete(until_param)) if params[until_param]
       table_name = class_type.table_name
 
-      if class_type == Event
+      if class_type.new.is_a?(Event)
         scope = scope.where("#{table_name}.#{attribute} >= ?", from_date) unless from_date.nil?
         scope = scope.where("#{table_name}.#{attribute} <= ?", until_date) unless until_date.nil?
       else 
@@ -381,9 +381,10 @@ module Api
     end
 
     def select_filtered_collection_of(object, method_or_relation, params)
-      assoc_class = extract_associated_classname(object, method_or_relation)
 
       conditions = make_conditions_with_parameter(params)
+      assoc_class = extract_associated_classname(object, method_or_relation, conditions)
+
       order = make_order_with_parameters(params, assoc_class)
       timestamp = make_timestamp_with_parameters_and_method(params, assoc_class)
 
@@ -563,11 +564,11 @@ module Api
     end
     private
 
-    def extract_associated_classname(object, method_or_relation)
+    def extract_associated_classname(object, method_or_relation, conditions)
       if is_a_relation?(method_or_relation)
-        method_or_relation.blank? ? '' : method_or_relation.first.class
+        method_or_relation.blank? ? '' : method_or_relation.where(conditions).first.class
       else
-        object.send(method_or_relation).first.class
+        object.send(method_or_relation).where(conditions).first.class
       end
     end
 


=====================================
test/api/articles_test.rb
=====================================
@@ -904,6 +904,19 @@ class ArticlesTest < ActiveSupport::TestCase
     assert_equal json.first['id'], article.id
   end
 
+  should 'return descendent of event articles from start_date' do
+    Article.delete_all
+    class EventDescendent < Event; end
+    article = fast_create(EventDescendent, :profile_id => user.person.id, :name => "Some thing", start_date: DateTime.now + 1)
+    fast_create(Event, :profile_id => user.person.id, :name => "Some thing", start_date: DateTime.now - 1)
+    params[:from_start_date] = DateTime.now
+    get "/api/v1/articles/?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_equal 1, json.length
+    assert_equal json.first['id'], article.id
+  end
+
+
   should 'return event articles from end_date' do
     Article.delete_all
     fast_create(Event, :profile_id => user.person.id, end_date: DateTime.now + 2)



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/fd38b6f86a58e877641bab4546a83bcea0929ff3...81f8cd1d5e0868799bb5239b3b0ae615587954cb

-- 
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/fd38b6f86a58e877641bab4546a83bcea0929ff3...81f8cd1d5e0868799bb5239b3b0ae615587954cb
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20181207/15a8ae4c/attachment-0001.html>


More information about the Noosfero-dev mailing list