class MeetingsController < ApplicationController
  before_filter :authenticate_user!
  before_filter :safe_select_group_and_meeting
  
  # GET /meetings
  # GET /meetings.json
  def index
    @meetings = Meeting.all

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

  # GET /meetings/1
  # GET /meetings/1.json
  def show

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

  # GET /meetings/new
  # GET /meetings/new.json
  def new
    @meeting = Meeting.new

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

  # GET /meetings/1/edit
  def edit
  end

  # POST /meetings
  # POST /meetings.json
  def create
    @meeting = Meeting.new(params[:meeting])

    respond_to do |format|
      if @meeting.save
        format.html { redirect_to [@group, @meeting], notice: 'Meeting was successfully created.' }
        format.json { render json: @meeting, status: :created, location: [@group, @meeting] }
      else
        format.html { render action: "new" }
        format.json { render json: @meeting.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /meetings/1
  # PUT /meetings/1.json
  def update

    respond_to do |format|
      if @meeting.update_attributes(params[:meeting])
        format.html { redirect_to [@group, @meeting], notice: 'Meeting was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @meeting.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /meetings/1
  # DELETE /meetings/1.json
  def destroy
    @meeting.destroy

    respond_to do |format|
      format.html { redirect_to group_meetings_url(@group) }
      format.json { head :no_content }
    end
  end
  
private
  
  def safe_select_group_and_meeting
    return unless user_signed_in?
    @group = current_user.groups.find(params[:group_id])
    @meetings = @group.meetings
    @meeting = @group.meetings.find(params[:id])  if params[:id]
  end
  
end
