Created
November 28, 2012 10:58
-
-
Save tuwukee/4160503 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SubscriptionDecorator < Draper::Base | |
include SubscriptionHelper | |
include ActionView::Helpers::TagHelper | |
decorates :subscription | |
attr_accessor :current_timeline, :today, :current_period, :term_factor, | |
:last_timeline_in_period, :updated_date, :current_event_type | |
def initialize(subscription, options = {}) | |
super | |
self.today = options[:today] || Date.today | |
self.current_timeline = timelines.where("date(created_at) <= ?", self.today).last | |
self.current_event_type = current_timeline.event_type if current_timeline | |
end | |
def active? | |
current_timeline.present? && current_event_type != EventType[:unsubscribed] | |
end | |
def type_mark | |
if current_timeline.present? | |
case | |
when StatusSuccessPolicy.success_status? then success_mark | |
when pending_status? then pending_mark | |
else fail_mark | |
end | |
end | |
end | |
private | |
def current_period_range(date) | |
case discipline.time_frame | |
when TimeFrame[:month] | |
date.beginning_of_month..date.end_of_month | |
when TimeFrame[:week] | |
date.beginning_of_week..date.end_of_week | |
else date..date | |
end | |
end | |
def success_status? | |
init_data_for_success | |
done_on_previous_dates? || done_today? | |
end | |
def pending_status? | |
init_data_for_pending | |
not_done_yet? || just_subscribed? || done_on_following_dates? | |
end | |
def done_on_following_dates? | |
current_period.include?(last_timeline_in_period ? last_timeline_in_period.updated_at.to_date : false) | |
end | |
def done_on_previous_dates? | |
today >= updated_date && event_type_besides_subscribed? && | |
current_period.include?(updated_date) | |
end | |
def not_done_yet? | |
current_period_range(Date.today).first.to_date <= today && term_factor <= 0 | |
end | |
def just_subscribed? | |
today == Date.today && term_factor == 0 | |
end | |
def done_today? | |
event_type_besides_subscribed? && term_factor == 0 | |
end | |
def event_type_besides_subscribed? | |
current_event_type != EventType[:subscribed] | |
end | |
def init_data_for_success | |
self.current_period = current_period_range(today) | |
self.updated_date = current_timeline.updated_at.to_date | |
self.term_factor = updated_date - today | |
end | |
def init_data_for_pending | |
self.last_timeline_in_period = timelines.where("date(created_at) <= ? AND event_type = ?", | |
current_period.last.to_date, EventType[:github_event]).last | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment