Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.
/ diversify Public archive

Commit

Permalink
RSpec: metrics_helper.rb (100%)
Browse files Browse the repository at this point in the history
  • Loading branch information
Juneezee committed Dec 16, 2019
1 parent f5f74c9 commit 578a9ac
Show file tree
Hide file tree
Showing 31 changed files with 188 additions and 229 deletions.
7 changes: 7 additions & 0 deletions app/assets/stylesheets/metrics_page.scss
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ aside.menu {
}
}
}

#graph-div {
min-height: 340px;
display: flex;
align-items: center;
justify-content: center;
}
}


Expand Down
23 changes: 1 addition & 22 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,15 @@ class ApplicationController < ActionController::Base

after_action :track_action

## The following are used by our Responder service classes so we can access
## the instance variable for the current resource easily via a standard method
def resource_name
controller_name.demodulize.singularize
end

def current_resource
instance_variable_get(:"@#{resource_name}")
end

def current_resource=(val)
instance_variable_set(:"@#{resource_name}", val)
end

# Catch NotFound exceptions and handle them neatly, when URLs are mistyped or mislinked
rescue_from ActiveRecord::RecordNotFound do
render template: 'errors/error_404', status: 404
end

rescue_from CanCan::AccessDenied do
render template: 'errors/error_403', status: 403
end

# IE over HTTPS will not download if browser caching is off, so allow browser caching when sending files
def send_file(file, opts = {})
response.headers['Cache-Control'] = 'private, proxy-revalidate' # Still prevent proxy caching
response.headers['Pragma'] = 'cache'
response.headers['Expires'] = '0'
super(file, opts)
end

protected

# Ahoy Gem function to track actions
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/metrics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def update_graph_time
config[:data] = data

# Check if there is still valid data, else return "No Data"
if helpers.there_data?(data)
if helpers.has_data?(data)
return_partial('#graph-div', layout, config)
else
return_partial(nil, nil, {})
Expand Down Expand Up @@ -76,8 +76,8 @@ def generate_json_response(title, template, locals)
end

# set
def setter(option)
[helpers.data_setter(option), helpers.config_setter(option)]
def setter(graph)
[helpers.data_setter(graph), helpers.config_setter(graph)]
end

private
Expand All @@ -98,4 +98,4 @@ def extra_processing(name, data)
def graph_params
params.require(:metric)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/newsletters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def create_unsubscribe
end

def subscribers
@subscribers = NewsletterSubscription.where(subscribed: true)
@subscribers = NewsletterSubscription.where(subscribed: true).decorate
end

private
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class NewsletterDecorator < Draper::Decorator
# frozen_string_literal: true

# Decorate for Newsletter
class NewsletterSubscriptionDecorator < Draper::Decorator
delegate_all

# Define presentation-specific methods here. Helpers are accessed through
Expand All @@ -10,4 +13,7 @@ class NewsletterDecorator < Draper::Decorator
# end
# end

def created_at
l object.created_at, format: :date
end
end
13 changes: 0 additions & 13 deletions app/decorators/project_decorator.rb

This file was deleted.

13 changes: 0 additions & 13 deletions app/decorators/review_decorator.rb

This file was deleted.

13 changes: 0 additions & 13 deletions app/decorators/task_decorator.rb

This file was deleted.

13 changes: 0 additions & 13 deletions app/decorators/team_decorator.rb

This file was deleted.

31 changes: 14 additions & 17 deletions app/helpers/metrics_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ module MetricsHelper
Referrers: { group_by: 'referrer', time: 'started_at', average: nil }
}.freeze

def config_setter(option)
val = GRAPH_CONFIG.stringify_keys.keys.select { |key| option.include? key }
puts "VAL #{val}"
GRAPH_CONFIG.stringify_keys.fetch(val[0],
{ time: 'created_at', average: nil, group_by: nil } )
def config_setter(graph)
val = GRAPH_CONFIG.stringify_keys.keys.select { |key| graph.include? key }

GRAPH_CONFIG.stringify_keys.fetch(
val[0], time: 'created_at', average: nil, group_by: nil
)
end

def data_setter(option)
case option
def data_setter(graph)
case graph
when /Reason/
[{ title: 'Reason', data: NewsletterFeedback.graph }]
when /Landing Page/
Expand Down Expand Up @@ -62,11 +63,8 @@ def decide_layout(option)
end

# loops through arrays and check if there is at least one array has data
def there_data?(array)
array.each do |data|
return true if data[:data].any?
end
false
def has_data?(array)
array.any? { |data| data[:data].any? }
end

# set time_constraint to data based on the number of time constraint selected
Expand All @@ -88,11 +86,10 @@ def time_constraint(time, data)

def custom_date_scope(data, date1, date2)
data.select do |v|
if date2.nil?
v[:created_at].between?(date1, date1 + 1.days)
else
v[:created_at].between?(date1, DateTime.parse(date2) + 1.days)
end
v[:created_at].between?(
date1,
date2.nil? ? date1 + 1.days : DateTime.parse(date2) + 1.days
)
end
end
end
18 changes: 0 additions & 18 deletions app/inputs/date_string_input.rb

This file was deleted.

25 changes: 0 additions & 25 deletions app/inputs/inline_check_boxes_input.rb

This file was deleted.

25 changes: 0 additions & 25 deletions app/inputs/inline_radio_buttons_input.rb

This file was deleted.

6 changes: 2 additions & 4 deletions app/models/concerns/date_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ module DateScope

included do
scope :on_date, lambda { |query, time|
where(query + ' BETWEEN ? AND ?',
time, time + 1.days)
where(query + ' BETWEEN ? AND ?', time, time + 1.days)
}

scope :between_date, lambda { |query, time1, time2|
where(query + ' BETWEEN ? AND ?',
time1, time2 + 1.days)
where(query + ' BETWEEN ? AND ?', time1, time2 + 1.days)
}
end
end
17 changes: 9 additions & 8 deletions app/models/newsletter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
class Newsletter < ApplicationRecord
include DateScope

scope :graph, -> { find_by_sql(
"SELECT newsletters.title,
newsletters.created_at, COUNT(newsletter_feedbacks)
as feedback_count FROM newsletters JOIN newsletter_feedbacks
ON newsletter_feedbacks.created_at BETWEEN newsletters.created_at
AND newsletters.created_at+interval\'7 days\' GROUP BY newsletters.id"
)
}
scope :graph, lambda {
find_by_sql(
"SELECT newsletters.title,
newsletters.created_at, COUNT(newsletter_feedbacks)
as feedback_count FROM newsletters JOIN newsletter_feedbacks
ON newsletter_feedbacks.created_at BETWEEN newsletters.created_at
AND newsletters.created_at+interval\'7 days\' GROUP BY newsletters.id"
)
}

validates_presence_of :title, :content
end
13 changes: 7 additions & 6 deletions app/models/newsletter_subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
#
# Table name: newsletter_subscriptions
#
# id :bigint not null, primary key
# date_subscribed :date default(Mon, 16 Dec 2019), not null
# email :string not null
# subscribed :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# id :bigint not null, primary key
# email :string not null
# subscribed :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
Expand All @@ -27,6 +26,8 @@ class NewsletterSubscription < ApplicationRecord
scope :all_subscribed_emails, -> { where(subscribed: true).pluck(:email) }
scope :previously_subscribed, -> { where(subscribed: false) }



def self.send_newsletter(newsletter)
all_subscribed_emails.each_slice(50) do |emails|
NewsletterMailer.send_newsletter(emails, newsletter).deliver_later
Expand Down
2 changes: 1 addition & 1 deletion app/views/newsletters/subscribers.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
- @subscribers.each do |subscriber|
%tr{id: "subscriber#{subscriber.id}"}
%td= subscriber.email
%td= subscriber.date_subscribed
%td= subscriber.created_at
%td= link_to 'Unsubscribe', unsubscribe_newsletters_path(email: subscriber.email, admin: 1),
method: 'post', data: { confirm: 'Are you sure?' }, remote: true, class: 'delete_sub'
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ en:
full: "%a %e %B %Y"
time:
formats:
date: "%d/%m/%Y"
full: "%a %e %B %Y at %H:%M"
compact: "%d/%m/%y at %H:%M"
short: "%d/%m/%Y %H:%M"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveDateSubscribedFromNewsletterSubscriptions < ActiveRecord::Migration[6.0]
def change
remove_column :newsletter_subscriptions, :date_subscribed
end
end
3 changes: 1 addition & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_12_16_022348) do
ActiveRecord::Schema.define(version: 2019_12_16_174911) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -149,7 +149,6 @@

create_table "newsletter_subscriptions", force: :cascade do |t|
t.string "email", null: false
t.date "date_subscribed", default: "2019-12-16", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.boolean "subscribed", default: true, null: false
Expand Down
4 changes: 0 additions & 4 deletions spec/decorators/newsletter_decorator_spec.rb

This file was deleted.

Loading

0 comments on commit 578a9ac

Please sign in to comment.