Class: Apidae::ObjectsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/apidae/objects_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#apidae_user, #check_user_data!, #user_has_data?, #user_is_admin?

Instance Method Details

#createObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/apidae/objects_controller.rb', line 34

def create
  @obj = Obj.new(object_params)
  if Obj.find_by_apidae_id(@obj.apidae_id)
    redirect_to objects_url, alert: "Cet objet est déjà importé." and return
  elsif !params[:selection_apidae_id].blank?
    selection = Selection.find_by_apidae_id(params[:selection_apidae_id])
    if selection && selection.valid_api? && @obj.save && selection.add_or_refresh_obj(@obj.apidae_id)
      redirect_to objects_url, notice: "L'objet a bien été importé" and return
    end
  end
  flash[:alert] = "Une erreur s'est produite lors de l'import de l'objet."
  render :new
end

#destroyObject



56
57
58
59
# File 'app/controllers/apidae/objects_controller.rb', line 56

def destroy
  @obj.destroy
  redirect_to objects_url, notice: 'Object was successfully destroyed.'
end

#editObject



31
32
# File 'app/controllers/apidae/objects_controller.rb', line 31

def edit
end

#indexObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/apidae/objects_controller.rb', line 8

def index
  session[:referrer] = request.referrer
  if params[:selection_id]
    @selection = Selection.find(params[:selection_id])
    @objects = @selection.objects.select(:id, :apidae_id, :title_data, :apidae_type, :updated_at)
  elsif user_is_admin?
    @objects = Obj.all.select(:id, :apidae_id, :title_data, :apidae_type, :updated_at)
  else
    projects_ids = Project.where(apidae_id: apidae_user.apidae_projects_ids).map {|p| p.id}
    @objects = Obj.joins(:selections).where("apidae_selections.apidae_project_id IN (?)", projects_ids)
                 .select("apidae_objs.id, apidae_objs.apidae_id, apidae_objs.title_data, apidae_objs.apidae_type, apidae_objs.updated_at")
                 .distinct("apidae_objs.apidae_id").to_a
  end
end

#newObject



26
27
28
29
# File 'app/controllers/apidae/objects_controller.rb', line 26

def new
  @obj = Obj.new
  @selection_apidae_id = params[:selection_apidae_id]
end

#refreshObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/apidae/objects_controller.rb', line 61

def refresh
  referrer = (session.delete(:referrer) || objects_url)
  begin
    sel = @obj.selections.find {|s| s.valid_api?}
    if sel && sel.add_or_refresh_obj(@obj.apidae_id)
      redirect_to referrer, notice: "L'objet touristique a bien été mis à jour."
    else
      redirect_to referrer, alert: "Une erreur s'est produite lors de la mise à jour de l'objet."
    end
  rescue OpenURI::HTTPError => err
    logger.error("Failed to refresh obj : #{@obj.apidae_id}")
    logger.error("Error is : #{err}")
    redirect_to referrer, alert: "Une erreur s'est produite lors de la mise à jour de l'objet. Veuillez vérifier que le projet Apidae comporte une clé API valide."
  rescue Exception => ex
    logger.error("Failed to refresh obj : #{@obj.apidae_id}")
    logger.error("Error is : #{err}")
    redirect_to referrer, alert: "Une erreur s'est produite lors de la mise à jour de l'objet."
  end
end

#showObject



23
24
# File 'app/controllers/apidae/objects_controller.rb', line 23

def show
end

#updateObject



48
49
50
51
52
53
54
# File 'app/controllers/apidae/objects_controller.rb', line 48

def update
  if @obj.update(object_params)
    redirect_to @obj, notice: 'Object was successfully updated.'
  else
    render :edit
  end
end