Skip to content

Instantly share code, notes, and snippets.

@aaronmcadam
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronmcadam/11394346 to your computer and use it in GitHub Desktop.
Save aaronmcadam/11394346 to your computer and use it in GitHub Desktop.
module ApplicationHelper
def tenant_name
tenant_configuration.tenant_name
end
def full_title(page_title)
base_title = "#{app_display_name} Dashboard"
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}".html_safe
end
end
def app_display_name
tenant_configuration.display_name
end
def participant_login_link
tenant_configuration.disc_url
end
def analytics
render("layouts/analytics") if Rails.env.production?
end
def display_header?
HeaderDisplayPolicy.call(controller: controller_name, action: action_name)
end
def time_from(date)
from_time = DateTime.parse(date)
distance_of_time_in_words(from_time, DateTime.now) << " ago"
end
def support_email_link
link_to(t("support"), "mailto:#{support_email}")
end
def terms_link
link_to(t("terms"), page_path("terms"))
end
def website_link
link_to(website_link_text, website_url, target: "_blank")
end
def copyright
copy = "&copy; #{t("copyright")} #{copyright_agencies} #{copyright_period}"
copy.html_safe
end
def complete_on_device
t("task.complete_on_device",
app_name: app_display_name,
ios: ios_link,
android: android_link
).html_safe
end
def current_language_flag
language = Language.all.select { |l| l.code == I18n.locale.to_s }.first
image_tag(language.flag, alt: language.label)
end
private
def website_link_text
strip_www(URI.parse(website_url).host)
end
def strip_www(host)
host.start_with?("www.") ? host[4..-1] : host
end
def website_url
tenant_configuration.website
end
def support_email
tenant_configuration.support_email
end
def copyright_agencies
tenant_configuration.copyright
end
def copyright_period
"2012-#{Time.now.year}"
end
def ios_link
link_to("iOS", tenant_configuration.ios_url, target: "_blank")
end
def android_link
link_to("Android", tenant_configuration.android_url, target: "_blank")
end
def tenant_configuration
Hashie::Mash.new(Crowdlab.tenants[Crowdlab.tenant])
end
end
require "spec_helper"
describe ApplicationHelper do
# ...
describe "#display_header?" do
it "creates a new HeaderDisplayPolicy instance and calls #call" do
args = { controller: helper.controller_name, action: helper.action_name }
policy_double = double
allow(HeaderDisplayPolicy).to receive(:new).with(args)
.and_return(policy_double)
expect(policy_double).to receive(:call)
helper.display_header?
end
end
# ...
end
class HeaderDisplayPolicy
def self.call(controller:, action:)
new(controller: controller, action: action).call
end
def initialize(controller:, action:)
@controller = controller
@action = action
end
def call
projects_index? || controller_in_whitelist?
end
private
attr_reader :controller, :action
def projects_index?
controller == "projects" && action == "index"
end
def controller_in_whitelist?
white_list.include?(controller)
end
def white_list
%w(sessions users passwords pages joins)
end
end
require "spec_helper"
describe HeaderDisplayPolicy do
describe ".call" do
it "creates a new instance and calls #call" do
args = { controller: "projects", action: "index" }
policy_double = double
allow(HeaderDisplayPolicy).to receive(:new).with(args)
.and_return(policy_double)
expect(policy_double).to receive(:call)
HeaderDisplayPolicy.call(args)
end
end
describe "#call" do
context "when @controller is 'projects' and @action is 'index'" do
it "returns true" do
result = HeaderDisplayPolicy.new(controller: "projects",
action: "index").call
expect(result).to eq(true)
end
end
context "when @controller is in the white list" do
it "returns true" do
result = HeaderDisplayPolicy.new(controller: "sessions",
action: "index").call
expect(result).to eq(true)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment