Class: AgentsController

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

Instance Method Summary (collapse)

Methods inherited from ApplicationController

can_access?, permission_mappings, set_access_control

Instance Method Details

- (Object) create



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'frontend/app/controllers/agents_controller.rb', line 38

def create
  handle_crud(:instance => :agent,
              :model => JSONModel(@agent_type),
              :find_opts => find_opts,
              :on_invalid => ->(){
                return render_aspace_partial :partial => "agents/new" if inline?
                return render :action => :new
              },
              :on_valid => ->(id){
                return render :json => @agent.to_hash if inline?
                return redirect_to({:controller => :agents, :action => :new, :agent_type => @agent_type}, :flash => {:success => I18n.t("agent._frontend.messages.created")}) if params.has_key?(:plus_one)
                redirect_to({:controller => :agents, :action => :edit, :id => id, :agent_type => @agent_type}, :flash => {:success => I18n.t("agent._frontend.messages.created")})
              })
end

- (Object) defaults



87
88
89
90
91
92
93
94
95
# File 'frontend/app/controllers/agents_controller.rb', line 87

def defaults
  defaults = DefaultValues.get params['agent_type']

  @agent = JSONModel(@agent_type).new({:agent_type => @agent_type})._always_valid!

  @agent.update(defaults.form_values) if defaults

  render 'defaults'
end

- (Object) delete



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'frontend/app/controllers/agents_controller.rb', line 72

def delete
  agent = JSONModel(@agent_type).find(params[:id])

  begin
    agent.delete
  rescue ConflictException => e
    flash[:error] = e.conflicts
    redirect_to(:controller => :agents, :action => :show, :id => params[:id])
    return
  end

  flash[:success] = I18n.t("agent._frontend.messages.deleted", JSONModelI18nWrapper.new(:agent => agent))
  redirect_to(:controller => :agents, :action => :index, :deleted_uri => agent.uri)
end

- (Object) edit



34
35
36
# File 'frontend/app/controllers/agents_controller.rb', line 34

def edit
  @agent = JSONModel(@agent_type).find(params[:id], find_opts)
end

- (Object) index



11
12
13
# File 'frontend/app/controllers/agents_controller.rb', line 11

def index
  @search_data = Search.for_type(session[:repo_id], "agent", {"sort" => "title_sort asc"}.merge(params_for_backend_search.merge({"facet[]" => SearchResultData.AGENT_FACETS})))
end

- (Object) merge



119
120
121
122
123
124
# File 'frontend/app/controllers/agents_controller.rb', line 119

def merge
  handle_merge( params[:refs],
                JSONModel(@agent_type).uri_for(params[:id]),
                'agent',
                {:agent_type => @agent_type})
end

- (Object) new



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'frontend/app/controllers/agents_controller.rb', line 19

def new
  @agent = JSONModel(@agent_type).new({:agent_type => @agent_type})._always_valid!
  if user_prefs['default_values']
    defaults = DefaultValues.get @agent_type.to_s

    @agent.update(defaults.values) if defaults
  end

  if @agent.names.empty?
    @agent.names = [@name_type.new({:authorized => true, :is_display_name => true})._always_valid!]
  end

  render_aspace_partial :partial => "agents/new" if inline?
end

- (Object) show



15
16
17
# File 'frontend/app/controllers/agents_controller.rb', line 15

def show
  @agent = JSONModel(@agent_type).find(params[:id], find_opts)
end

- (Object) update



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'frontend/app/controllers/agents_controller.rb', line 53

def update
  handle_crud(:instance => :agent,
              :model => JSONModel(@agent_type),
              :obj => JSONModel(@agent_type).find(params[:id], find_opts),
              :on_invalid => ->(){

                if @agent.names.empty?
                  @agent.names = [@name_type.new._always_valid!]
                end

                return render :action => :edit
              },
              :on_valid => ->(id){
                flash[:success] = I18n.t("agent._frontend.messages.updated")
                redirect_to :controller => :agents, :action => :edit, :id => id, :agent_type => @agent_type
              })
end

- (Object) update_defaults



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'frontend/app/controllers/agents_controller.rb', line 97

def update_defaults

  begin

    DefaultValues.from_hash({
                              "record_type" => @agent_type.to_s,
                              "lock_version" => params['agent'].delete('lock_version'),
                              "defaults" => cleanup_params_for_schema(
                                                                      params['agent'],
                                                                      JSONModel(@agent_type).schema)
                            }).save

    flash[:success] = I18n.t("default_values.messages.defaults_updated")
    redirect_to :controller => :agents, :action => :defaults
  rescue Exception => e
    flash[:error] = e.message
    redirect_to :controller => :agents, :action => :defaults
  end
end