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



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

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



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

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



79
80
81
# File 'app/controllers/shelves_controller.rb', line 79

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

#indexObject

GET /shelves GET /shelves.json



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

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: 'shelf_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
      else
        order_by sort[:sort_by], sort[:order]
      end
      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



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

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



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

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

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

#updateObject

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



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

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(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