require_dependency "phcmemberspro/application_controller" module Phcmemberspro class Directory::ListingsController < ApplicationController # Security & Action Filters before_action :require_user before_action :set_paper_trail_whodunnit before_action :directory_category_information before_action :set_directory_listing, only: [:show, :edit, :update, :destroy] layout 'layouts/phcmemberspro/directory/directory_all.html.erb' # Directory Listing Index def index category = Directory::Category.find(params[:category_id]) @directory_listings = category.listings.where(oganization_id: membership_info.org_id) end # Show Directory Listing def show category = Directory::Category.find(params[:category_id]) @directory_listing = category.listings.find(params[:id]) end # New Directory Listing def new category = Directory::Category.find(params[:category_id]) @directory_listing = category.listings.build end # Edit Directory Listing Action def edit end # Create Directory Listing Action def create @category = Directory::Category.find(params[:category_id]) @directory_listing = @category.listings.create(directory_listing_params) @directory_listing.user_id = current_user.id @directory_listing.membership_id = membership_info.id @directory_listing.oganization_id = membership_info.org_id if @directory_listing.save redirect_to directory_category_listings_path, notice: 'Listing was successfully created.' else render :new end end # Update Directory Listing Action def update @directory_listing.user_id = current_user.id @directory_listing.membership_id = membership_info.id @directory_listing.oganization_id = membership_info.org_id if @directory_listing.update(directory_listing_params) redirect_to directory_category_listings_path, notice: 'Listing was successfully updated.' else render :edit end end # Delete Directory Listing def destroy @category = Directory::Category.find(params[:category_id]) @directory_listing = @category.listings.find(params[:id]) @directory_listing.destroy redirect_to directory_category_listings_path, notice: 'Listing was successfully destroyed.' end private # Directory Category Information def directory_category_information @directory_category = Directory::Category.find(params[:category_id]) end # Grab User Session Key (For ID) def current_user @_current_user ||= AuthRocket::Session.from_token(session[:ar_token]).try(:user) end # Get Current User from Above and Get Membership Info def membership_info AuthRocket::Membership.all(user_id: current_user.id).first end # Common Callbacks def set_directory_listing @directory_listing = Directory::Listing.find(params[:id]) end # Whitelists def directory_listing_params params.require(:directory_listing).permit(:business_id, :category_id, :main_id, :user_id, :membership_id, :oganization_id) end end end