From 16e0fb11010895df919a9ab9e4a94984aa4d8bef Mon Sep 17 00:00:00 2001 From: jacobperia Date: Fri, 13 Oct 2023 15:05:32 -0400 Subject: [PATCH] use super from parent class --- app/components/matey/active_users_component.rb | 2 +- app/components/matey/application_component.rb | 11 ++++++----- .../matey/browser_os_breakdown_component.rb | 2 +- app/components/matey/daily_active_users_component.rb | 2 +- app/components/matey/new_activity_component.rb | 2 +- app/components/matey/new_users_component.rb | 2 +- app/components/matey/top_events_component.rb | 2 +- .../matey/top_visited_pages_table_component.rb | 2 +- app/components/matey/user_engagement_component.rb | 2 +- .../matey/visits_by_day_of_week_component.rb | 2 +- 10 files changed, 15 insertions(+), 14 deletions(-) diff --git a/app/components/matey/active_users_component.rb b/app/components/matey/active_users_component.rb index 7db91ac..a773d71 100644 --- a/app/components/matey/active_users_component.rb +++ b/app/components/matey/active_users_component.rb @@ -2,7 +2,7 @@ class Matey::ActiveUsersComponent < Matey::ApplicationComponent def initialize(events:, time_window: 1.week, color_scheme: "neutral") - validate_arguments(records: events, time_window: time_window) + super(records: events, time_window: time_window) @current_period = events.where(time: time_window.ago..Time.current).pluck(:user_id).uniq.count previous_period = events.where(time: (2 * time_window).ago..time_window.ago).pluck(:user_id).uniq.count diff --git a/app/components/matey/application_component.rb b/app/components/matey/application_component.rb index 7883b18..b900bee 100644 --- a/app/components/matey/application_component.rb +++ b/app/components/matey/application_component.rb @@ -6,12 +6,13 @@ class Matey::ApplicationComponent < ViewComponent::Base include ActiveModel::Validations include ColorSchemeHelper - def before_render - validate! - end - - def validate_arguments(records:, time_window:) + def initialize(records:, time_window:) + super raise ArgumentError unless records.is_a?(ActiveRecord::Relation) raise ArgumentError unless time_window.is_a?(Integer) end + + def before_render + validate! + end end diff --git a/app/components/matey/browser_os_breakdown_component.rb b/app/components/matey/browser_os_breakdown_component.rb index ca1aa71..b8d3bf6 100644 --- a/app/components/matey/browser_os_breakdown_component.rb +++ b/app/components/matey/browser_os_breakdown_component.rb @@ -1,6 +1,6 @@ class Matey::BrowserOsBreakdownComponent < Matey::ApplicationComponent def initialize(visits:, time_window:, color_scheme: "neutral") - validate_arguments(records: visits, time_window: time_window) + super(records: visits, time_window: time_window) visits_in_time_window = visits.where(started_at: time_window.ago..) @visits_in_time_window = visits_in_time_window.count diff --git a/app/components/matey/daily_active_users_component.rb b/app/components/matey/daily_active_users_component.rb index 67c43bf..0c58975 100644 --- a/app/components/matey/daily_active_users_component.rb +++ b/app/components/matey/daily_active_users_component.rb @@ -1,6 +1,6 @@ class Matey::DailyActiveUsersComponent < Matey::ApplicationComponent def initialize(visits:, time_window:, color_scheme: "neutral") - validate_arguments(records: visits, time_window: time_window) + super(records: visits, time_window: time_window) @visits = visits @time_window = time_window diff --git a/app/components/matey/new_activity_component.rb b/app/components/matey/new_activity_component.rb index 73879ae..88ca3f4 100644 --- a/app/components/matey/new_activity_component.rb +++ b/app/components/matey/new_activity_component.rb @@ -1,6 +1,6 @@ class Matey::NewActivityComponent < Matey::ApplicationComponent def initialize(events:, time_window: 1.week, color_scheme: "neutral") - validate_arguments(records: events, time_window: time_window) + super(records: events, time_window: time_window) @current_period = events.where(time: time_window.ago..Time.current).count previous_period = events.where(time: (2 * time_window).ago..time_window.ago).pluck(:user_id).count diff --git a/app/components/matey/new_users_component.rb b/app/components/matey/new_users_component.rb index 8e87506..04f3fc2 100644 --- a/app/components/matey/new_users_component.rb +++ b/app/components/matey/new_users_component.rb @@ -1,6 +1,6 @@ class Matey::NewUsersComponent < Matey::ApplicationComponent def initialize(users:, time_window: 1.week, color_scheme: "neutral") - validate_arguments(records: users, time_window: time_window) + super(records: users, time_window: time_window) @current_period = users.where(created_at: time_window.ago..Time.current).count previous_period = users.where(created_at: (2 * time_window).ago..time_window.ago).count diff --git a/app/components/matey/top_events_component.rb b/app/components/matey/top_events_component.rb index 385adb6..29a3d5e 100644 --- a/app/components/matey/top_events_component.rb +++ b/app/components/matey/top_events_component.rb @@ -2,7 +2,7 @@ class Matey::TopEventsComponent < Matey::ApplicationComponent def initialize(events:, time_window: 1.week, limit: 5, color_scheme: "neutral") - validate_arguments(records: events, time_window: time_window) + super(records: events, time_window: time_window) @events = events.where(time: time_window.ago..Time.current).limit(limit).order("count(name) DESC").group(:name).count @time_window = time_window diff --git a/app/components/matey/top_visited_pages_table_component.rb b/app/components/matey/top_visited_pages_table_component.rb index a7197bf..fb0af0c 100644 --- a/app/components/matey/top_visited_pages_table_component.rb +++ b/app/components/matey/top_visited_pages_table_component.rb @@ -1,6 +1,6 @@ class Matey::TopVisitedPagesTableComponent < Matey::ApplicationComponent def initialize(events:, time_window: 1.week, limit: 10, color_scheme: "neutral") - validate_arguments(records: events, time_window: time_window) + super(records: events, time_window: time_window) # Group events by controller (:name) and action. Aggregate number of unique user actions @user_count_by_event = events.where(started_at: time_window.ago..).pluck(:landing_page).tally diff --git a/app/components/matey/user_engagement_component.rb b/app/components/matey/user_engagement_component.rb index e9c9db6..5b7a8ad 100644 --- a/app/components/matey/user_engagement_component.rb +++ b/app/components/matey/user_engagement_component.rb @@ -2,7 +2,7 @@ class Matey::UserEngagementComponent < Matey::ApplicationComponent def initialize(events:, user_id:, time_window: 1.week, limit: 10, color_scheme: "neutral") - validate_arguments(records: events, time_window: time_window) + super(records: events, time_window: time_window) @events_for_user = events.where_props(user_id: user_id).where(time: time_window.ago..Time.current).group(:name).count @count_by_event = @events_for_user.sort_by { |event, count| count }.last(limit).reverse diff --git a/app/components/matey/visits_by_day_of_week_component.rb b/app/components/matey/visits_by_day_of_week_component.rb index f93122c..2c04883 100644 --- a/app/components/matey/visits_by_day_of_week_component.rb +++ b/app/components/matey/visits_by_day_of_week_component.rb @@ -3,7 +3,7 @@ class Matey::VisitsByDayOfWeekComponent < Matey::ApplicationComponent def initialize(visits:, time_window: 1.month, exclude_days: [], color_scheme: "neutral") - validate_arguments(records: visits, time_window: time_window) + super(records: visits, time_window: time_window) @visits = visits @time_window = time_window