Class: Vitrage::PiecesController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- Vitrage::PiecesController
- Defined in:
- app/controllers/vitrage/pieces_controller.rb
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #new ⇒ Object
- #reorder ⇒ Object
- #restore_order ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
Instance Method Details
#create ⇒ Object
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/vitrage/pieces_controller.rb', line 30 def create error_state = nil # check existance of params unless params[:kind] && VitrageOwnersPiecesSlot::PIECE_CLASSES_STRINGS.include?(params[:kind]) && params[:owner_type] && params[:owner_id] error_state = { descr: "Necessary parameters didn't exist error" } end # get the owner of vitrage if error_state.nil? @owner = nil begin @owner = Object.const_get(params[:owner_type]).find params[:owner_id] rescue Exception => e error_state = { descr: "Owner find error" } end end # create piece if error_state.nil? @piece = VitragePieces.const_get(params[:kind]).new @piece.assign_attributes vitrage_piece_params unless @piece.save error_state = { descr: "Piece save error", piece: @piece.class.name.demodulize.underscore, errors: @piece.errors } end end # create vitrage slot if error_state.nil? @slot = VitrageOwnersPiecesSlot.new owner: @owner, piece: @piece unless @slot.save error_state = { descr: "Slot save error" } @piece.destroy end end if error_state respond_to do |format| format.html { render json: error_state.merge({ status: 'error' }), status: :unprocessable_entity } end else respond_to do |format| format.html { render layout: false } format.js # for remotipart gem correct work (ajax multipart form) end # render layout: false end end |
#destroy ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'app/controllers/vitrage/pieces_controller.rb', line 107 def destroy @slot = VitrageOwnersPiecesSlot.find params[:id] @slot.destroy respond_to do |format| format.html { render text: "" } end end |
#edit ⇒ Object
24 25 26 27 28 |
# File 'app/controllers/vitrage/pieces_controller.rb', line 24 def edit respond_to do |format| format.html { render layout: false } end end |
#new ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'app/controllers/vitrage/pieces_controller.rb', line 11 def new piece_class = params[:kind] unless piece_class && VitrageOwnersPiecesSlot::PIECE_CLASSES_STRINGS.include?(piece_class) piece_class = VitrageOwnersPiecesSlot::PIECE_CLASSES_STRINGS.first end @piece = VitragePieces.const_get(piece_class).new respond_to do |format| format.html { render layout: false } end end |
#reorder ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'app/controllers/vitrage/pieces_controller.rb', line 116 def reorder moved_slot = VitrageOwnersPiecesSlot.find params[:id] wrong_params = false if params[:beforeid].present? if params[:beforeid] == "end" max_ordn = moved_slot.owner.vitrage_slots.maximum(:ordn) moved_slot.update_attributes ordn: max_ordn ? max_ordn + 1 : 1 else slot_after_moved = VitrageOwnersPiecesSlot.find_by_id params[:beforeid] if slot_after_moved.present? && slot_after_moved.owner == moved_slot.owner ordn_for_moved = slot_after_moved.ordn all_slots_after_moved = moved_slot.owner.vitrage_slots. where("vitrage_owners_pieces_slots.ordn >= ?", ordn_for_moved). where.not(id: moved_slot.id). order(ordn: :asc) all_slots_after_moved.each do |slot| slot.update_attributes ordn: slot.ordn + 1 end moved_slot.update_attributes ordn: ordn_for_moved else wrong_params = true end end else wrong_params = true end if wrong_params render text: "wrong params", status: :unprocessable_entity else render text: "ok" end end |
#restore_order ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'app/controllers/vitrage/pieces_controller.rb', line 153 def restore_order owners = VitrageOwnersPiecesSlot.all.collect { |vp| vp.owner }.uniq owners.each do |owner| owner.vitrage_slots.each_with_index do |slot, indx| slot.update_attributes ordn: indx + 1 end end render text: "ok" end |
#show ⇒ Object
5 6 7 8 9 |
# File 'app/controllers/vitrage/pieces_controller.rb', line 5 def show respond_to do |format| format.html { render layout: false } end end |
#update ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/controllers/vitrage/pieces_controller.rb', line 87 def update if @piece.update(vitrage_piece_params) respond_to do |format| format.html { render text: "" } format.js { render text: "" } end else error_state = { status: 'error', descr: "Piece save error", piece: @piece.class.name.demodulize.underscore, errors: @piece.errors } respond_to do |format| format.html { render json: error_state, status: :unprocessable_entity } format.js { render json: error_state, status: :unprocessable_entity } end end end |