Skip to content

Commit

Permalink
refactoring + introduced CookieNag (just a stub for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchy committed Sep 20, 2011
1 parent c696c7e commit 93c15d9
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 59 deletions.
53 changes: 35 additions & 18 deletions app/helpers/noodnik/nags_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,52 @@ module Noodnik
module NagsHelper
def nag_user_to(topic, &block)
nag = find_nag(topic)
return if nag && (nag.next_nag > Time.now || nag.completed?)

return if nag && !nag.due?
begin
class_eval('alias :original_link_to :link_to')
class_eval('alias :link_to :my_custom_link_to')
Context.new.local_eval do
@noodnik_topic = topic
content_tag :div, class: 'noodnik-nag' do
capture(&block)
end
end
ensure
@noodnik_topic = nil
class_eval('alias :link_to :original_link_to')
end
alias_methods
yield_block(topic, &block)
ensure
cleanup
end
end

private

def find_nag(topic)
attr = { user_id: Noodnik.current_user_id.call, topic: topic }
Nag.find :first, conditions: attr
user_id = Noodnik.current_user_id.call

if user_id
attr = { user_id: user_id, topic: topic }
Nag.find :first, conditions: attr
else
CookieNag.new
end
end

def alias_methods
class_eval('alias :original_link_to :link_to')
class_eval('alias :link_to :my_custom_link_to')
end

def yield_block(topic, &block)
Context.new.local_eval do
@noodnik_topic = topic
content_tag :div, class: 'noodnik-nag' do
capture(&block)
end
end
end

def cleanup
@noodnik_topic = nil
class_eval('alias :link_to :original_link_to')
end

def my_custom_link_to(*args)
has_options = args.last.is_a? Hash
html_options = has_options ? args.last : {}
html_options["data-noodnik-complete-path"] = noodnik.routes.url_helpers.complete_path(topic: @noodnik_topic)

if html_options.include? :class
html_options[:class] += " noodnik-complete"
else
Expand All @@ -43,7 +60,7 @@ def my_custom_link_to(*args)

class Context
include Rails.application.routes.mounted_helpers
def postpone_for(period)
def postpone_for(period)
original_link_to "Remind me in #{period.inspect}", noodnik.routes.url_helpers.postpone_path(period: period, topic: @noodnik_topic), class: 'noodnik-postpone'
end
end
Expand Down
15 changes: 15 additions & 0 deletions app/models/noodnik/nag.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
module Noodnik
class Nag < ActiveRecord::Base
def due?
!(postponed? || completed?)
end

private

def postponed?
next_nag > Time.now
end
end

class CookieNag
def due?
true
end
end
end
6 changes: 0 additions & 6 deletions test/dummy/spec/controllers/noodnik_nags_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,6 @@
end
end

def set_current_user_id(user_id)
Noodnik.setup do |config|
config.current_user_id = lambda { user_id }
end
end

def stub_time
t = Time.parse("01/01/2010 10:00")
Time.stub!(:now).and_return(t)
Expand Down
118 changes: 83 additions & 35 deletions test/dummy/spec/helpers/nags_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,97 @@
describe "nag_user_to" do
before :each do
@topic = :register
@user_id = 1
Noodnik.setup do |config|
config.current_user_id = lambda { @user_id }
end
end

after :each do
Noodnik::Nag.delete_all
end
describe "when signed in" do
before :each do
@user_id = 1
set_current_user_id(@user_id)
end

it "yields the block in a div with class 'noodnik-nag'" do
helper.nag_user_to :register do |nag|
"I should be in a <div>!"
end.should match(%r[<div class="noodnik-nag">.*</div>])
end
after :each do
Noodnik::Nag.delete_all
end

it "should yield the block for a new topic" do
helper.nag_user_to :register do |nag|
"Register!"
end.should include("Register!")
end
it "yields the block in a div with class 'noodnik-nag'" do
helper.nag_user_to :register do |nag|
"I should be in a <div>!"
end.should match(%r[<div class="noodnik-nag">.*</div>])
end

it "should not yield the block if postponed" do
Noodnik::Nag.create! user_id: @user_id, topic: @topic, next_nag: 2.weeks.from_now
it "yields the block for a new topic" do
helper.nag_user_to :register do |nag|
"Register!"
end.should include("Register!")
end

helper.nag_user_to :register do |nag|
"I should not be returned!"
end.should be_nil
end
it "does not yield the block if topic has been postponed" do
Noodnik::Nag.create! user_id: @user_id, topic: @topic, next_nag: 2.weeks.from_now

helper.nag_user_to :register do |nag|
"I should not be returned!"
end.should be_nil
end

it "should yield the block if postpone expired" do
Noodnik::Nag.create! user_id: @user_id, topic: @topic, next_nag: 1.week.ago
it "yields the block if postpone expired" do
Noodnik::Nag.create! user_id: @user_id, topic: @topic, next_nag: 1.week.ago

helper.nag_user_to :register do |nag|
"Register!"
end.should include("Register!")
helper.nag_user_to :register do |nag|
"Register!"
end.should include("Register!")
end

it "does not yield the block if topic was completed" do
Noodnik::Nag.create! user_id: @user_id, topic: @topic, next_nag: 10.weeks.ago, completed: true

helper.nag_user_to :register do |nag|
"I should not be returned!"
end.should be_nil
end
end

it "should not yield the block if completed" do
Noodnik::Nag.create! user_id: @user_id, topic: @topic, next_nag: 10.weeks.ago, completed: true
describe "when not signed in" do
before :each do
set_current_user_id(nil)
@cookies = mock('cookies')
controller.stub!(:cookies).and_return(@cookies)
end

it "yields the block in a div with class 'noodnik-nag'" do
helper.nag_user_to :register do |nag|
"I should be in a <div>!"
end.should match(%r[<div class="noodnik-nag">.*</div>])
end

helper.nag_user_to :register do |nag|
"I should not be returned!"
end.should be_nil
it "yields the block for a new topic" do
helper.nag_user_to :register do |nag|
"Register!"
end.should include("Register!")
end

it "does not yield the block if topic has been postponed" do
# Noodnik::Nag.create! user_id: @user_id, topic: @topic, next_nag: 2.weeks.from_now

helper.nag_user_to :register do |nag|
"I should not be returned!"
end.should be_nil
end

it "yields the block if postpone expired" do
# Noodnik::Nag.create! user_id: @user_id, topic: @topic, next_nag: 1.week.ago

helper.nag_user_to :register do |nag|
"Register!"
end.should include("Register!")
end

it "does not yield the block if topic was completed" do
# Noodnik::Nag.create! user_id: @user_id, topic: @topic, next_nag: 10.weeks.ago, completed: true

helper.nag_user_to :register do |nag|
"I should not be returned!"
end.should be_nil
end
end

end
Expand Down Expand Up @@ -77,7 +124,7 @@
end
end

it "adds 'data-noodnik-complete-path' with the correct topic when no html_options provided" do
it "adds 'data-noodnik-complete-path' with the correct topic" do
@link.should include('data-noodnik-complete-path="/noodnik/complete?topic=register"')
end

Expand Down Expand Up @@ -110,10 +157,11 @@
@link.should include("topic=register")
end

it "should have class 'noodnik-postpone'" do
it "has class 'noodnik-postpone'" do
@link.should include("noodnik-postpone")
end

# postpone_for is the only type of link inside a nag_user_to block that *should'nt* get class 'noodnik-complete'
it "does not add class 'noodnik-complete'" do
@link.should_not include("noodnik-complete")
end
Expand Down
7 changes: 7 additions & 0 deletions test/dummy/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
require 'rubygems'
require 'spork'


def set_current_user_id(user_id)
Noodnik.setup do |config|
config.current_user_id = lambda { user_id }
end
end

Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
Expand Down

0 comments on commit 93c15d9

Please sign in to comment.