|
| 1 | +class ListingsController < ApplicationController |
| 2 | + # This must stay **before** cancan methods, because they do a Listing.new(listing_params) |
| 3 | + # but new tags break it |
| 4 | + before_action :create_new_tags, only: [:create, :update] |
| 5 | + |
| 6 | + load_and_authorize_resource |
| 7 | + before_action :set_commoner, only: [:new, :create, :destroy] |
| 8 | + |
| 9 | + # GET /listings |
| 10 | + # GET /listings.json |
| 11 | + def index |
| 12 | + @listings = Listing.all |
| 13 | + end |
| 14 | + |
| 15 | + # GET /listings/1 |
| 16 | + # GET /listings/1.json |
| 17 | + def show |
| 18 | + end |
| 19 | + |
| 20 | + # GET /listings/new |
| 21 | + def new |
| 22 | + @listing = @commoner.listings.build |
| 23 | + end |
| 24 | + |
| 25 | + # GET /listings/1/edit |
| 26 | + def edit |
| 27 | + end |
| 28 | + |
| 29 | + # POST /listings |
| 30 | + # POST /listings.json |
| 31 | + def create |
| 32 | + @listing = @commoner.listings.build(listing_params) |
| 33 | + respond_to do |format| |
| 34 | + if @listing.save |
| 35 | + format.html { redirect_to @listing, notice: 'Listing was successfully created.' } |
| 36 | + format.json { render :show, status: :created, location: @listing } |
| 37 | + else |
| 38 | + format.html { render :new } |
| 39 | + format.json { render json: @listing.errors, status: :unprocessable_entity } |
| 40 | + end |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + # PATCH/PUT /listings/1 |
| 45 | + # PATCH/PUT /listings/1.json |
| 46 | + def update |
| 47 | + respond_to do |format| |
| 48 | + if @listing.update(listing_params) |
| 49 | + format.html { redirect_to @listing, notice: 'Listing was successfully updated.' } |
| 50 | + format.json { render :show, status: :ok, location: @listing } |
| 51 | + else |
| 52 | + format.html { render :edit } |
| 53 | + format.json { render json: @listing.errors, status: :unprocessable_entity } |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + # DELETE /listings/1 |
| 59 | + # DELETE /listings/1.json |
| 60 | + def destroy |
| 61 | + @listing.destroy |
| 62 | + respond_to do |format| |
| 63 | + format.html { redirect_to listings_url, notice: 'Listing was successfully destroyed.' } |
| 64 | + format.json { head :no_content } |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + private |
| 69 | + # Use callbacks to share common setup or constraints between actions. |
| 70 | + def set_listing |
| 71 | + @listing = Listing.find(params[:id]) |
| 72 | + end |
| 73 | + |
| 74 | + # Never trust parameters from the scary internet, only allow the white list through. |
| 75 | + def listing_params |
| 76 | + params.require(:listing).permit(:title, :description, :place, :min_price, :max_price, tag_ids: []) |
| 77 | + end |
| 78 | + |
| 79 | + def set_commoner |
| 80 | + @commoner = params[:commoner_id].present? ? params[:commoner_id] : current_user.meta |
| 81 | + end |
| 82 | + |
| 83 | + # This method creates the not-yet-existing tags and replaces |
| 84 | + # tag_ids with their ids. |
| 85 | + def create_new_tags |
| 86 | + if params[:listing][:tag_ids].present? |
| 87 | + params[:listing][:tag_ids].map! do |tag_id| |
| 88 | + if Tag.exists? tag_id |
| 89 | + tag_id |
| 90 | + else |
| 91 | + new_tag = Tag.create(name: tag_id.downcase) |
| 92 | + new_tag.id |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | +end |
0 commit comments