Class: AgentsController

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

Instance Method Summary collapse

Instance Method Details

#createObject

POST /agents POST /agents.json



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

def create
  @agent = Agent.new(agent_params)

  respond_to do |format|
    if @agent.save
      case
      when @work
        @agent.works << @work
      when @manifestation
        @agent.manifestations << @manifestation
      when @item
        @agent.items << @item
      end
      format.html { redirect_to @agent, notice: t('controller.successfully_created', model: t('activerecord.models.agent')) }
      format.json { render json: @agent, status: :created, location: @agent }
    else
      prepare_options
      format.html { render action: "new" }
      format.json { render json: @agent.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /agents/1 DELETE /agents/1.json



191
192
193
194
195
196
197
198
# File 'app/controllers/agents_controller.rb', line 191

def destroy
  @agent.destroy

  respond_to do |format|
    format.html { redirect_to agents_url, notice: t('controller.successfully_deleted', model: t('activerecord.models.agent')) }
    format.json { head :no_content }
  end
end

#editObject

GET /agents/1/edit



145
146
147
# File 'app/controllers/agents_controller.rb', line 145

def edit
  prepare_options
end

#indexObject

GET /agents GET /agents.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/agents_controller.rb', line 11

def index
  if params[:mode] == 'add'
    unless current_user.try(:has_role?, 'Librarian')
      access_denied; return
    end
  end
  query = params[:query].to_s.strip

  if query.size == 1
    query = "#{query}*"
  end

  @query = query.dup
  query = query.gsub(' ', ' ')
  order = nil
  @count = {}

  search = Agent.search(include: [:agent_type, :required_role])
  search.data_accessor_for(Agent).select = [
    :id,
    :full_name,
    :full_name_transcription,
    :agent_type_id,
    :required_role_id,
    :created_at,
    :updated_at,
    :date_of_birth
  ]
  set_role_query(current_user, search)

  if params[:mode] == 'recent'
    query = "#{query} created_at_d:[NOW-1MONTH TO NOW]"
  end
  if query.present?
    search.build do
      fulltext query
    end
  end

  unless params[:mode] == 'add'
    work = @work
    expression = @expression
    manifestation = @manifestation
    agent = @agent
    agent_merge_list = @agent_merge_list
    search.build do
      with(:work_ids).equal_to work.id if work
      with(:expression_ids).equal_to expression.id if expression
      with(:manifestation_ids).equal_to manifestation.id if manifestation
      with(:original_agent_ids).equal_to agent.id if agent
      with(:agent_merge_list_ids).equal_to agent_merge_list.id if agent_merge_list
    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].to_i || 1
  page = 1 if page == 0
  search.query.paginate(page, Agent.default_per_page)
  @agents = search.execute!.results

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

  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render xml: @agents }
    format.rss  { render layout: false }
    format.atom
    format.json
    format.html.phone
  end
end

#newObject

GET /agents/new GET /agents/new.json



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

def new
  @agent = Agent.new
  @agent.required_role = Role.where(name: 'Guest').first
  @agent.language = Language.where(iso_639_1: I18n.default_locale.to_s).first || Language.first
  @agent.country = current_user.profile.library.country
  prepare_options

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

#showObject

GET /agents/1 GET /agents/1.json



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/agents_controller.rb', line 89

def show
  case
  when @work
    @agent = @work.creators.find(params[:id])
  when @manifestation
    @agent = @manifestation.publishers.find(params[:id])
  when @item
    @agent = @item.agents.find(params[:id])
  else
    if @version
      @agent = @agent.versions.find(@version).item if @version
    end
  end

  agent = @agent
  role = current_user.try(:role) || Role.default_role
  @works = Manifestation.search do
    with(:creator_ids).equal_to agent.id
    with(:required_role_id).less_than_or_equal_to role.id
    paginate page: params[:work_list_page], per_page: Manifestation.default_per_page
  end.results
  @expressions = Manifestation.search do
    with(:contributor_ids).equal_to agent.id
    with(:required_role_id).less_than_or_equal_to role.id
    paginate page: params[:expression_list_page], per_page: Manifestation.default_per_page
  end.results
  @manifestations = Manifestation.search do
    with(:publisher_ids).equal_to agent.id
    with(:required_role_id).less_than_or_equal_to role.id
    paginate page: params[:manifestation_list_page], per_page: Manifestation.default_per_page
  end.results

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @agent }
    format.js
    format.html.phone
  end
end

#updateObject

PUT /agents/1 PUT /agents/1.json



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

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