Commit e30dab143da2636bb3ab5a5335c7dceaff77a58e

Authored by Leandro Santos
1 parent 8e1ebf5b
Exists in fix_sign_up_form

should limit the number of activities returned by activities endpoint

app/api/helpers.rb
... ... @@ -187,6 +187,28 @@ module Api
187 187 present_partial tasks, :with => Entities::Task
188 188 end
189 189  
  190 + ###########################
  191 + # Activities #
  192 + ###########################
  193 + def find_activities(asset, method_or_relation = 'activities')
  194 +
  195 + not_found! if asset.blank? || asset.secret || !asset.visible
  196 + forbidden! if !asset.display_private_info_to?(current_person)
  197 +
  198 + activities = select_filtered_collection_of(asset, method_or_relation, params)
  199 + activities = activities.map(&:activity)
  200 + activities
  201 + end
  202 +
  203 + def present_activities_for_asset(asset, method_or_relation = 'activities')
  204 + tasks = find_activities(asset, method_or_relation)
  205 + present_activities(tasks)
  206 + end
  207 +
  208 + def present_activities(activities)
  209 + present_partial activities, :with => Entities::Activity, :current_person => current_person
  210 + end
  211 +
190 212 def make_conditions_with_parameter(params = {})
191 213 parsed_params = parser_params(params)
192 214 conditions = {}
... ... @@ -210,7 +232,7 @@ module Api
210 232 order = 'RANDOM()'
211 233 else
212 234 field_name, direction = params[:order].split(' ')
213   - assoc_class = extract_associated_classname(method_or_relation)
  235 + assoc_class = extract_associated_classname(object, smethod_or_relation)
214 236 if !field_name.blank? and assoc_class
215 237 if assoc_class.attribute_names.include? field_name
216 238 if direction.present? and ['ASC','DESC'].include? direction.upcase
... ... @@ -223,12 +245,12 @@ module Api
223 245 return order
224 246 end
225 247  
226   - def make_timestamp_with_parameters_and_method(params, method_or_relation)
  248 + def make_timestamp_with_parameters_and_method(object, method_or_relation, params)
227 249 timestamp = nil
228 250 if params[:timestamp]
229 251 datetime = DateTime.parse(params[:timestamp])
230   - table_name = extract_associated_tablename(method_or_relation)
231   - assoc_class = extract_associated_classname(method_or_relation)
  252 + table_name = extract_associated_tablename(object, method_or_relation)
  253 + assoc_class = extract_associated_classname(object, method_or_relation)
232 254 date_atrr = assoc_class.attribute_names.include?('updated_at') ? 'updated_at' : 'created_at'
233 255 timestamp = "#{table_name}.#{date_atrr} >= '#{datetime}'"
234 256 end
... ... @@ -258,7 +280,7 @@ module Api
258 280 def select_filtered_collection_of(object, method_or_relation, params)
259 281 conditions = make_conditions_with_parameter(params)
260 282 order = make_order_with_parameters(object,method_or_relation,params)
261   - timestamp = make_timestamp_with_parameters_and_method(params, method_or_relation)
  283 + timestamp = make_timestamp_with_parameters_and_method(object, method_or_relation, params)
262 284  
263 285 objects = is_a_relation?(method_or_relation) ? method_or_relation : object.send(method_or_relation)
264 286 objects = by_reference(objects, params)
... ... @@ -406,15 +428,15 @@ module Api
406 428 end
407 429 private
408 430  
409   - def extract_associated_tablename(method_or_relation)
410   - extract_associated_classname(method_or_relation).table_name
  431 + def extract_associated_tablename(object, method_or_relation)
  432 + extract_associated_classname(object, method_or_relation).table_name
411 433 end
412 434  
413   - def extract_associated_classname(method_or_relation)
  435 + def extract_associated_classname(object, method_or_relation)
414 436 if is_a_relation?(method_or_relation)
415 437 method_or_relation.blank? ? '' : method_or_relation.first.class
416 438 else
417   - method_or_relation.to_s.singularize.camelize.constantize
  439 + object.send(method_or_relation).table_name.singularize.camelize.constantize
418 440 end
419 441 end
420 442  
... ...
app/api/v1/activities.rb
... ... @@ -5,13 +5,8 @@ module Api
5 5 resource :profiles do
6 6  
7 7 get ':id/activities' do
8   - profile = Profile.find_by id: params[:id]
9   -
10   - not_found! if profile.blank? || profile.secret || !profile.visible
11   - forbidden! if !profile.display_private_info_to?(current_person)
12   -
13   - activities = profile.activities.map(&:activity)
14   - present activities, :with => Entities::Activity, :current_person => current_person
  8 + profile = environment.profiles.find_by id: params[:id]
  9 + present_activities_for_asset(profile)
15 10 end
16 11 end
17 12 end
... ...
test/api/activities_test.rb
... ... @@ -131,9 +131,62 @@ class ActivitiesTest < ActiveSupport::TestCase
131 131 assert_nil json["activities"].last['content']
132 132 end
133 133  
  134 + should 'list activities with pagination' do
  135 + ActionTracker::Record.destroy_all
  136 + a1 = create_activity(:target => person)
  137 + a2 = create_activity(:target => person)
  138 +
  139 + params[:page] = 1
  140 + params[:per_page] = 1
  141 + get "/api/v1/profiles/#{person.id}/activities?#{params.to_query}"
  142 + json_page_one = JSON.parse(last_response.body)
  143 +
  144 + params[:page] = 2
  145 + params[:per_page] = 1
  146 + get "/api/v1/profiles/#{person.id}/activities?#{params.to_query}"
  147 + json_page_two = JSON.parse(last_response.body)
  148 +
  149 + assert_includes json_page_one["activities"].map { |a| a["id"] }, a2.id
  150 + assert_not_includes json_page_one["activities"].map { |a| a["id"] }, a1.id
  151 +
  152 + assert_includes json_page_two["activities"].map { |a| a["id"] }, a1.id
  153 + assert_not_includes json_page_two["activities"].map { |a| a["id"] }, a2.id
  154 + end
  155 +
  156 + should 'list only 20 elements by page if no limit param is passed' do
  157 + ActionTracker::Record.destroy_all
  158 + 1.upto(25).map do
  159 + create_activity(:target => person)
  160 + end
  161 + get "/api/v1/profiles/#{person.id}/activities?#{params.to_query}"
  162 + json = JSON.parse(last_response.body)
  163 + assert_equal 20, json["activities"].length
  164 + end
  165 +
  166 + should 'list activities with timestamp' do
  167 + ActionTracker::Record.destroy_all
  168 + a1 = create_activity(:target => person)
  169 + a2 = create_activity(:target => person)
  170 + a2.updated_at = Time.zone.now
  171 + a2.save
  172 +
  173 + a1.updated_at = Time.zone.now + 3.hours
  174 + a1.save!
  175 +
  176 +
  177 + params[:timestamp] = Time.zone.now + 1.hours
  178 + get "/api/v1/profiles/#{person.id}/activities?#{params.to_query}"
  179 + json = JSON.parse(last_response.body)
  180 +
  181 + assert_includes json["activities"].map { |a| a["id"] }, a1.id
  182 + assert_not_includes json["activities"].map { |a| a["id"] }, a2.id
  183 + end
  184 +
  185 +
  186 +
134 187 def create_activity(params = {})
135 188 params[:verb] ||= 'create_article'
136   - ActionTracker::Record.create! :verb => params[:verb], :user => person, :target => params[:target]
  189 + ActionTracker::Record.create!(params.merge(:user => person))
137 190 end
138 191  
139 192 end
... ...