Class: ShelvesController

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

Instance Method Summary collapse

Instance Method Details

#createObject

POST /shelves POST /shelves.json



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/shelves_controller.rb', line 83

def create
  @shelf = Shelf.new(shelf_params)
  if @library
    @shelf.library = @library
  else
    @shelf.library = Library.web #unless current_user.has_role?('Librarian')
  end

  respond_to do |format|
    if @shelf.save
      format.html { redirect_to @shelf, notice: t('controller.successfully_created', model: t('activerecord.models.shelf')) }
      format.json { render json: @shelf, status: :created, location:  @shelf }
    else
      @library = Library.first if @shelf.library.nil?
      format.html { render action: "new" }
      format.json { render json: @shelf.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /shelves/1 DELETE /shelves/1.json



128
129
130
131
132
133
134
135
# File 'app/controllers/shelves_controller.rb', line 128

def destroy
  @shelf.destroy

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

#editObject

GET /shelves/1/edit



77
78
79
# File 'app/controllers/shelves_controller.rb', line 77

def edit
  @shelf = Shelf.includes(:library).find(params[:id])
end

#indexObject

GET /shelves GET /shelves.json



8
9
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
# File 'app/controllers/shelves_controller.rb', line 8

def index
  if params[:mode] == 'select'
    if @library
      @shelves = @library.shelves
    else
      @shelves = Shelf.real.order(:position)
    end
    render partial: 'select_form'
    return
  else
    sort = {sort_by: 'name', order: 'asc'}
    #case params[:sort_by]
    #when 'name'
    #  sort[:sort_by] = 'name'
    #end
    sort[:order] = 'desc' if params[:order] == 'desc'

    query = @query = params[:query].to_s.strip
    page = params[:page] || 1
    library = @library if @library

    search = Shelf.search(include: [:library]) do
      fulltext query if query.present?
      paginate page: page.to_i, per_page: Shelf.default_per_page
      if library
        with(:library).equal_to library.name
        order_by :position, :asc
      end
      order_by sort[:sort_by], sort[:order]
      facet :library
    end
    @shelves = search.results
    @library_facet = search.facet(:library).rows
    @library_names = Library.pluck(:name)
  end

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @shelves }
  end
end

#newObject

GET /shelves/new GET /shelves/new.json



64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/shelves_controller.rb', line 64

def new
  @shelf = Shelf.new
  @library = current_user.profile.library unless @library
  @shelf.library = @library
  #@shelf.user = current_user

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

#showObject

GET /shelves/1 GET /shelves/1.json



52
53
54
55
56
57
58
59
60
# File 'app/controllers/shelves_controller.rb', line 52

def show
  @shelf = Shelf.includes(:library).find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @shelf }
    format.mobile
  end
end

#updateObject

PUT /shelves/1 PUT /shelves/1.json



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/shelves_controller.rb', line 105

def update
  @shelf.library = @library if @library

  if params[:move]
    move_position(@shelf, params[:move], false)
    redirect_to shelves_url(library_id: @shelf.library_id)
    return
  end

  respond_to do |format|
    if @shelf.update_attributes(shelf_params)
      format.html { redirect_to @shelf, notice: t('controller.successfully_updated', model: t('activerecord.models.shelf')) }
      format.json { head :no_content }
    else
      @library = Library.first if @library.nil?
      format.html { render action: "edit" }
      format.json { render json: @shelf.errors, status: :unprocessable_entity }
    end
  end
end