Class: SubjectsController

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

Instance Method Summary collapse

Instance Method Details

#createObject

POST /subjects POST /subjects.json



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/controllers/subjects_controller.rb', line 119

def create
  if @work
    @subject = @work.subjects.new(params[:subject])
  else
    @subject = Subject.new(params[:subject])
  end
  classification = Classification.find(@subject.classification_id) if @subject.classification_id.present?
  subject_heading_type = SubjectHeadingType.find(@subject.subject_heading_type_id) if @subject.subject_heading_type_id.present?

  respond_to do |format|
    if @subject.save
      #@subject.classifications << classification if classification
      @subject.subject_heading_types << subject_heading_type if subject_heading_type
#        @subject.subject_heading_type = subject_heading_type if subject_heading_type
      format.html { redirect_to @subject, :notice => t('controller.successfully_created', :model => t('activerecord.models.subject')) }
      format.json { render :json => @subject, :status => :created, :location => @subject }
    else
      @classification = classification
      @subject_heading_type = subject_heading_type
      prepare_options
      format.html { render :action => "new" }
      format.json { render :json => @subject.errors, :status => :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /subjects/1 DELETE /subjects/1.json



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/controllers/subjects_controller.rb', line 177

def destroy
  if @work
    @subject = @work.subjects.find(params[:id])
  #else
  #  @subject = Subject.find(params[:id])
  end
  @subject.destroy

  respond_to do |format|
    format.html { redirect_to subjects_url }
    format.json { head :no_content }
  end
end

#editObject

GET /subjects/1/edit



108
109
110
111
112
113
114
115
# File 'app/controllers/subjects_controller.rb', line 108

def edit
  if @work
    @subject = @work.subjects.find(params[:id])
  else
    @subject = Subject.find(params[:id])
    @subject.subject_heading_type_id = @subject.subject_heading_type.id if @subject.subject_heading_type.present?
  end
end

#indexObject

GET /subjects GET /subjects.json



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
# File 'app/controllers/subjects_controller.rb', line 11

def index
  sort = {:sort_by => 'created_at', :order => 'desc'}
  case params[:sort_by]
  when 'name'
    sort[:sort_by] = 'term'
  end
  sort[:order] = 'asc' if params[:order] == 'asc'

  search = Subject.search(:include => [:works])
  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[:sort_by], sort[:order]
  end

  unless params[:mode] == 'add'
    work = @work
    classification = @classification
    subject_heading_type = @subject_heading_type
    search.build do
      with(:work_ids).equal_to work.id if work
      with(:classification_ids).equal_to classification.id if classification
      with(:subject_heading_type_ids).equal_to subject_heading_type.id if subject_heading_type
    end
  end

  role = current_user.try(:role) || Role.default_role
  search.build do
    with(:required_role_id).less_than_or_equal_to role.id
  end

  page = params[:page] || 1
  search.query.paginate(page.to_i, Subject.default_per_page)
  @subjects = search.execute!.results
  session[:params] = {} unless session[:params]
  session[:params][:subject] = params

  flash[:page_info] = {:page => page, :query => query}

  respond_to do |format|
    format.html # index.html.erb
    format.json { render :json => @subjects }
    format.rss
    format.atom
  end
end

#newObject

GET /subjects/new



96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/subjects_controller.rb', line 96

def new
  @subject = Subject.new
  @subject.classification_id = @classification.id if @classification
  @subject.subject_heading_type_id = @subject_heading_type.id if @subject_heading_type

  respond_to do |format|
    format.html # new.html.erb
    format.json { render :json => @subject }
  end
end

#search_nameObject

GET /subjects/search_name.json



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/controllers/subjects_controller.rb', line 192

def search_name
  struct_subject = Struct.new(:id, :text, :term_transcription)
  if params[:subject_id]
     a = Subject.where(id: params[:subject_id]).select("id, term").first
     result = nil
     result = struct_subject.new(a.id, a.term)
  else
     subjects = Subject.where("term like '%#{params[:search_phrase]}%'").select("id, term, term_transcription").limit(10)
     result = []
     subjects.each do |subject|
         result << struct_subject.new(subject.id, subject.term, subject.term_transcription)
     end
  end
  respond_to do |format|
    format.json { render :text => result.to_json }
  end
end

#showObject

GET /subjects/1 GET /subjects/1.json



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
# File 'app/controllers/subjects_controller.rb', line 67

def show
  if params[:term]
    subject = Subject.where(:term => params[:term]).first
    redirected_to subject
    return
  end

  if @work
    @subject = @work.subjects.find(params[:id])
  #else
  #  @subject = Subject.find(params[:id])
  end
  search = Sunspot.new_search(Manifestation)
  subject = @subject
  search.build do
    with(:subject_ids).equal_to subject.id if subject
  end
  page = params[:work_page] || 1
  search.query.paginate(page.to_i, Manifestation.default_per_page)
  @works = search.execute!.results

  respond_to do |format|
    format.html # show.html.erb
    format.json { render :json => @subject }
    format.js
  end
end

#updateObject

PUT /subjects/1 PUT /subjects/1.json



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/controllers/subjects_controller.rb', line 147

def update
  if @work
    @subject = @work.subjects.find(params[:id])
  else
    @subject.assign_attributes(params[:subject])
  end

  classification = Classification.find(@subject.classification_id) if @subject.classification_id.present?
  subject_heading_type = SubjectHeadingType.find(@subject.subject_heading_type_id) if @subject.subject_heading_type_id.present?

  respond_to do |format|
    if @subject.save
      subject_heading_types = [] 
      subject_heading_types << subject_heading_type if subject_heading_type
      @subject.subject_heading_types = subject_heading_types

      format.html { redirect_to @subject, :notice => t('controller.successfully_updated', :model => t('activerecord.models.subject')) }
      format.json { head :no_content }
    else
      @classification = classification
      @subject_heading_type = subject_heading_type
      prepare_options
      format.html { render :action => "edit" }
      format.json { render :json => @subject.errors, :status => :unprocessable_entity }
    end
  end
end