OSDN Git Service

t#:
[laoz/laoz.git] / app / controllers / words_controller.rb
1 # 熟語
2 class WordsController < ApplicationController
3   layout 'default'
4   
5   private
6   
7   def get_show
8     @word = Word.find params[:id]
9     @section_count = SectionWord.count(:conditions => ['word_id = ?', @word.id]).to_i
10     @section_sum = SectionWord.sum(:count, :conditions => ['word_id = ?', @word.id]).to_i
11     @sections = Section.find(:all, :include => :section_words, 
12       :conditions => ['section_words.word_id = ?', @word.id]
13     )
14   end
15   
16   public
17   
18   def index
19     list
20     render :action => 'list'
21   end
22
23   # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
24   verify :method => :post, :only => [ :destroy, :create, :update, :upload_word, :remove_word ],
25          :redirect_to => { :action => :list }
26
27   def list
28     @page = params[:page].to_i
29     @page = 1 if @page < 1
30     @search = params[:search]
31     @filter = params[:filter].to_s.to_i
32     if @filter == 1
33       @words = Word.find(:all, :include => 'hateda_word', 
34         :conditions => ['hateda_words.upload_at is null or words.update_at > hateda_words.upload_at'] 
35       )
36     else
37       cond = if @search
38         ['letter = ?', @search]
39       else
40         nil
41       end
42       @paginate, @words = paginate(:words, :conditions => cond, 
43         :order => 'count desc', :per_page => 20
44       )
45     end
46     if @user
47       render :template => '/hateda/word_list'
48     else
49     end
50   end
51   
52   def show
53     get_show
54   end
55   
56   def new
57     @word = Word.new
58   end
59   
60   def create
61     @word = Word.new(params[:word])
62     if @word.save!
63       @word.parse
64       flash[:notice] = '登録しました'
65       redirect_to :action => :edit, :id => @word.id
66     else
67       render :action => 'new'
68     end
69   end
70   
71   def edit
72     @word = Word.find params[:id]
73     @section_count = SectionWord.count(:conditions => ['word_id = ?', @word.id]).to_i
74     @section_sum = SectionWord.sum(:count, :conditions => ['word_id = ?', @word.id]).to_i
75     @sections = Section.find(:all, :include => :section_words, 
76       :conditions => ['section_words.word_id = ?', @word.id]
77     )
78     @word.detail = @word.edit_detail
79   end
80   
81   def update
82     @word = Word.find(params[:id])
83     if @word.update_attributes(params[:word])
84       get_show
85       render :action => 'show', :id => @word.id
86     else
87       render :action => 'edit'
88     end
89   end
90   
91   def destroy
92     Word.find(params[:id]).destroy
93     redirect_to :action => 'list'
94   end
95   
96   def upload_word
97     @word = Word.find params[:id]
98     HatedaWord.upload_one @user, @pass, @word.id
99     get_show
100     render :action => :show, :id => @word.id
101   end
102   
103   def remove_word
104     htn = Hatena.new(@user, @pass)
105     @word = Word.find params[:id]
106     HatedaWord.remove_one htn, @word.id
107     get_show
108     render :action => :show, :id => @word.id
109   end
110   
111   #インポート実行
112   def import
113     e = nil
114     if (f = params[:browse][:file]).respond_to?(:read)
115       if f.size > 2.megabytes
116         e = 'ファイルサイズが2MBを超えています'
117       else
118         Word.import(f.read)
119       end
120     else
121       e = 'ファイルを受け取れませんでした'
122     end
123     if e
124       flash[:error] = 'インポートできませんでした'
125       flash[:txt_error] = e
126       redirect_to :controller => '/section', :action => :index
127     else
128       redirect_to :controller => '/section', :action => :index
129     end
130   end
131   
132   def export
133     send_data(Word.export, :filename => "word.txt")
134   end
135   
136 end