Skip to content

Instantly share code, notes, and snippets.

@wilhelser
Created January 17, 2014 21:11
Show Gist options
  • Save wilhelser/8481611 to your computer and use it in GitHub Desktop.
Save wilhelser/8481611 to your computer and use it in GitHub Desktop.
Logic for available issues
def get_current_available
property = self.property
frequency = property.frequency
@issues = []
@upcoming_issues = UpcomingIssue.where(:active => true)
@upcoming_issues.each do |i|
@issues << i.issue_month
end
case frequency
when 1
@months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
when 2
@months = ['January', 'March', 'May', 'July', 'September', 'November']
when 3
@months = ['February', 'April', 'June', 'August', 'October', 'December']
when 4
@months = ['January', 'April', 'July', 'October']
when 5
@months = ['February', 'May', 'August', 'November']
when 6
@months = ['March', 'June', 'September', 'December']
end
@final = @issues += @months
Rails.logger.info @final
@upcoming = @final.select{|item| @final.count(item) > 1}.uniq
if @upcoming.size > 1
@skip = []
if self.property.skip_next_issue?
@next = @upcoming.first
@skip << @next
end
unless self.newsletters.where(:status => "Abandoned", :issue_year => Date.today.year).last.nil?
@skip << self.newsletters.where(:status => "Abandoned", :issue_year => Date.today.year).last.issue_month
end
@complete = []
unless self.orders.last.nil?
@orders = self.orders.last(2)
@orders.each do |o|
if @upcoming.include?(o.issue_month)
@complete << o.issue_month
end
end
end
@newfinal = @upcoming += @complete += @skip
@newupcoming = @newfinal.select{|item| @newfinal.count(item) == 1}.uniq
@available = UpcomingIssue.where(:active => true).find_by_issue_month(@newupcoming)
else
@available = UpcomingIssue.where(:active => true).find_by_issue_month(@upcoming)
end
return @available
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment