add html titles to these pages
[kwestie.git] / app / controllers / issues_controller.rb
blobe0568054a578153673a3777b8de777012d095e28
1 class IssuesController < ApplicationController
3   helper :permalinks
4   include ActionView::Helpers::DateHelper
6   before_filter :login_required, :only => [ :new, :create, :edit, :update, :destroy, :comment]
8   # GET /issues
9   # GET /issues.xml
10   def index
11     @issues = Issue.find(:all)
13     respond_to do |format|
14       format.html # index.rhtml
15       format.xml  { render :xml => @issues.to_xml }
16     end
17   end
19   # GET /issues/ID
20   # GET /issues/ID.xml
21   def show
22     @issue = Issue.find(params[:id])
24     respond_to do |format|
25       format.html { redirect_to nice_permalink_url(@issue) }
26       format.xml  { render :xml => @issue.to_xml }
27     end
28   end
30   # GET /issues/YEAR/MO?/DA?
31   def find_by_date
32     dates = parse_date
33     @issues = Issue.find(:all, :conditions => [" created_at BETWEEN ? and ? ", dates["from"], dates["to"] ], 
34                               :order => :created_at)
35     flash[:message] = "Issues for %s %s from %s" %  [ dates["to"], distance_of_time_in_words( dates["requested"], dates["to"] ), dates["format"] ]
36     render :action => "index"
37   end
39   # GET /issues/YEAR/MO/DA/PERMALINK
40   def find_by_permalink
41     dates = parse_date
42     # FIXME: this finds the correct issue, but won't do the forward correctly so you can't act on an issue you find
43 #    @issues = Issue.find(:all, :joins => 'LEFT JOIN permalinks ON permalinks.issue_id = issues.id', :conditions => [" (issues.created_at BETWEEN ? and ?) and link_name = ? ", dates["from"], dates["to"], params[:permalink] ] )
45     @issues = Issue.find(:all, :include => [ :permalink, :created_by ], :conditions => [" (issues.created_at BETWEEN ? and ?) and link_name = ? ", dates["from"], dates["to"], params[:permalink] ] )
47     # FIXME: we should be smarter here where there is more than one issue with the same date and permalink name
48     @issue = @issues[0]
50     respond_to do |format|
51       format.html { render :action => "show" }
52       format.xml  { render :xml => @issue.to_xml }
53     end
54   end
56   # GET /issues/YEAR/MO/DA/PERMALINK/ID
57   def find_by_permalink_id
58     @issue = Issue.find(params[:id])
60     respond_to do |format|
61       format.html { render  :action => "show" }
62       format.xml  { render :xml => @issue.to_xml }
63     end
64   end
66   # GET /issues/new
67   def new
68     @issue = Issue.new
69   end
71   # GET /issues/1;edit
72   def edit
73     @issue = Issue.find(params[:id])
74   end
76   # GET /issues/comment/1
77   def comment
78     @issue = Issue.find(params[:id])
79     redirect_to issue_url(@issue)
80   end
82   # POST /issues
83   # POST /issues.xml
84   def create
85     @issue = Issue.new(params[:issue])
87     respond_to do |format|
88       if @issue.save
89         flash[:notice] = 'Issue was successfully created.'
90         format.html { redirect_to issue_url(@issue) }
91         format.xml  { head :created, :location => issue_url(@issue) }
92       else
93         format.html { render :action => "new" }
94         format.xml  { render :xml => @issue.errors.to_xml }
95       end
96     end
97   end
99   # PUT /issues/1
100   # PUT /issues/1.xml
101   def update
102     @issue = Issue.find(params[:id])
104     respond_to do |format|
105       if @issue.update_attributes(params[:issue])
106         flash[:notice] = 'Issue was successfully updated.'
107         format.html { redirect_to issue_url(@issue) }
108         format.xml  { head :ok }
109       else
110         format.html { render :action => "edit" }
111         format.xml  { render :xml => @issue.errors.to_xml }
112       end
113     end
114   end
116   # DELETE /issues/1
117   # DELETE /issues/1.xml
118   def destroy
119     @issue = Issue.find(params[:id])
120     @issue.destroy
122     respond_to do |format|
123       format.html { redirect_to issues_url }
124       format.xml  { head :ok }
125     end
126   end
128   protected
129     def parse_date
130       # adapted from http://www.therailsway.com/tags/idiomatic%20ruby
132       # here we take in whatever dates we get or use the 1st of the month or year if the date isn't present
133       requested_date = Date.new(params[:year].to_i, (params[:month] || 1).to_i, (params[:day] || 1).to_i)
135       # Date#- subtracts days from a given date, which lets us find
136       # the end of last month.
137       if ! params[:day]
138         from = requested_date - 1
139       else # correct in the ! params[:month] case as well
140         from = requested_date
141       end
143       # Date#>> lets us advance requested date by one month, 
144       # giving us the first of the next month.
146       if ! params[:day] # year and month
147         to = requested_date >> 1
148       elsif ! params[:month] # year only
149         to = Date.new(requested_date.year + 1, requested_date.month, requested_date.day)
150       else # year and month and day
151         to = requested_date + 1
152       end
154       requested_format = requested_date.to_s(:day)
155       requested_format = requested_date.to_s(:month) if ! params[:day]
156       requested_format = requested_date.to_s(:year)  if ! params[:month]
158       return { "to" => to, "from" => from, "requested" => requested_date ,"format" => requested_format }
159     end