summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/serializers/analytics_generic_entity.rb32
-rw-r--r--app/serializers/analytics_generic_serializer.rb3
2 files changed, 35 insertions, 0 deletions
diff --git a/app/serializers/analytics_generic_entity.rb b/app/serializers/analytics_generic_entity.rb
new file mode 100644
index 00000000000..b007d9056d0
--- /dev/null
+++ b/app/serializers/analytics_generic_entity.rb
@@ -0,0 +1,32 @@
+class AnalyticsGenericEntity < Grape::Entity
+ include RequestAwareEntity
+ include ActionView::Helpers::DateHelper
+
+
+ expose :title
+ expose :iid
+ expose :started_at, as: :date
+ expose :author, using: UserEntity
+
+ expose :total_time do |object|
+ distance_of_time_in_words(object[:total_time].to_f)
+ end
+
+ expose(:date) do |object|
+ interval_in_words(object[:created_at])
+ end
+
+ expose :url do |object|
+ url_to("namespace_project_#{object[:entity]}".to_sym, id: object[:iid].to_s)
+ end
+
+ private
+
+ def url_to(route, id = nil)
+ public_send("#{route}_url", options[:project].namespace, options[:project], id)
+ end
+
+ def interval_in_words(diff)
+ "#{distance_of_time_in_words(diff.to_f)} ago"
+ end
+end
diff --git a/app/serializers/analytics_generic_serializer.rb b/app/serializers/analytics_generic_serializer.rb
new file mode 100644
index 00000000000..2347b56a1d6
--- /dev/null
+++ b/app/serializers/analytics_generic_serializer.rb
@@ -0,0 +1,3 @@
+class AnalyticsGenericSerializer < BaseSerializer
+ entity AnalyticsGenericEntity
+end