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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/controllers/questions_controller.rb', line 131

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



164
165
166
167
168
169
170
171
# File 'app/controllers/questions_controller.rb', line 164

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



126
127
# File 'app/controllers/questions_controller.rb', line 126

def edit
end

#indexObject

GET /questions GET /questions.json



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
99
100
101
# File 'app/controllers/questions_controller.rb', line 10

def index
  store_location
  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



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

def new
  @question = current_user.questions.new
end

#showObject

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/questions_controller.rb', line 105

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



149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/controllers/questions_controller.rb', line 149

def update
  respond_to do |format|
    if @question.update_attributes(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