Class: QuestionsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/questions_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /questions POST /questions.json



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/questions_controller.rb', line 128

def create
  @question = Question.new(question_params)
  @question.user = current_user

  respond_to do |format|
    if @question.save
      flash[:notice] = t('controller.successfully_created', model: t('activerecord.models.question'))
      format.html { redirect_to @question }
      format.json { render json: @question, status: :created, location: @question }
    else
      format.html { render action: "new" }
      format.json { render json: @question.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /questions/1 DELETE /questions/1.json



161
162
163
164
165
166
167
168
# File 'app/controllers/questions_controller.rb', line 161

def destroy
  @question.destroy

  respond_to do |format|
    format.html { redirect_to user_questions_url(@question.user) }
    format.json { head :no_content }
  end
end

#editObject

GET /questions/1/edit



123
124
# File 'app/controllers/questions_controller.rb', line 123

def edit
end

#indexObject

GET /questions GET /questions.json



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/questions_controller.rb', line 8

def index
  if @user and user_signed_in?
    user = @user
  end
  c_user = current_user

  session[:params] = {} unless session[:params]
  session[:params][:question] = params

  @count = {}
  case params[:sort_by]
  when 'updated_at'
    sort_by = 'updated_at'
  when 'created_at'
    sort_by = 'created_at'
  when 'answers_count'
    sort_by = 'answers_count'
  else
    sort_by = 'updated_at'
  end

  case params[:solved]
  when 'true'
    solved = true
    @solved = solved.to_s
  when 'false'
    solved = false
    @solved = solved.to_s
  end

  search = Sunspot.new_search(Question)
  query = params[:query].to_s.strip
  unless query.blank?
    @query = query.dup
    query = query.gsub(' ', ' ')
    search.build do
      fulltext query
    end
  end
  search.build do
    order_by sort_by, :desc
  end

  search.build do
    with(:username).equal_to user.username if user
    if c_user != user
      unless c_user.has_role?('Librarian')
        with(:shared).equal_to true
      end
    end
    facet :solved
  end

  @question_facet = search.execute!.facet(:solved).rows

  if @solved
    search.build do
      with(:solved).equal_to solved
    end
  end

  page = params[:page] || 1
  search.query.paginate(page.to_i, Question.default_per_page)
  result = search.execute!
  @questions = result.results
  @count[:query_result] = @questions.total_entries

  if query.present?
    begin
      @crd_results = Question.search_crd(:query_01 => query, :page => params[:crd_page]).per(5)
    rescue Timeout::Error
      @crd_results = Kaminari::paginate_array([]).page(1)
    end
  end

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @questions }
    format.xml {
      if params[:mode] == 'crd'
        render template: 'questions/index_crd'
        convert_charset
      else
        render :xml => @questions
      end
    }
    format.rss  { render layout: false }
    format.atom
    format.js
  end
end

#newObject

GET /questions/new



118
119
120
# File 'app/controllers/questions_controller.rb', line 118

def new
  @question = current_user.questions.new
end

#showObject

GET /questions/1 GET /questions/1.json



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/questions_controller.rb', line 102

def show
  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @question }
    format.xml {
      if params[:mode] == 'crd'
        render template: 'questions/show_crd'
        convert_charset
      else
        render :xml => @question
      end
    }
  end
end

#updateObject

PUT /questions/1 PUT /questions/1.json



146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/controllers/questions_controller.rb', line 146

def update
  respond_to do |format|
    if @question.update(question_params)
      flash[:notice] = t('controller.successfully_updated', model: t('activerecord.models.question'))
      format.html { redirect_to @question }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @question.errors, status: :unprocessable_entity }
    end
  end
end