<repo_name>angeladowns/hellosasebo<file_sep>/README.md
# Hello, Sasebo!
January 2017

Ruby 2.6.3 // Rails 5.0.0.1

http://www.hellosasebo.com/

This adventure app was created using the Rails Girls LA Guide located at: https://gist.github.com/jendiamond/5a26b531e8e47b4aa638.
<file_sep>/lib/fog.rb
# lib/fog.rb

module Fog
  # :)
end
<file_sep>/spec/factories/comments.rb
FactoryBot.define do
  factory :comment do
    user_name "MyString"
    body "MyText"
    adventure_id 1
  end
end
<file_sep>/app/controllers/pages_controller.rb
class PagesController < ApplicationController
  def info
  end



  def map
  if params[:search].present?
    @adventures = Adventure.near(params[:search], 1000, :order => :address)
  else
    @adventures = Adventure.all
  end
    @hash = Gmaps4rails.build_markers(@adventures) do |adventure, marker|
    marker.lat adventure.latitude
    marker.lng adventure.longitude
    marker.infowindow adventure.name
    marker.picture({
      "width" => 32,
      "height" => 32})
    marker.json({ name: adventure.name})
   end
  end
end
<file_sep>/spec/requests/adventures_spec.rb
require 'rails_helper'

#------------------INDEX

describe "adventures", type: :request do
  let!(:adventure) { FactoryGirl.create(:adventure) }
  describe 'reading adventures' do
    it "should render adventures index template" do
      get '/adventures'
      expect(response).to have_http_status(200)
      expect(response).to render_template :index
    end
  end

#------------------NEW/CREATE

  describe 'GET /adventures/new' do
    it "should render adventures new template" do
      get '/adventures/new'
      expect(response).to have_http_status(200)
      expect(response).to render_template :new
    end
  end

  describe 'POST /adventures' do
    it 'should create a new adventure' do
      expect {
        post '/adventures', adventure: { date_time: adventure.date_time,
                                     activity: adventure.activity,
                                     location: adventure.location,
                                     description: adventure.description }
      }.to change(adventure, :count).by(1)
      expect(response).to have_http_status(302)
      expect(response).to redirect_to(adventure_url(adventure.last.id))
    end
  end


#-----NEW/CREATE FAILURE

  describe 'GET /adventures/:id save failure' do
    before do
        post '/adventures', adventure: { location: adventure.location,
                                     description: adventure.description }
    end
    it 'should not render adventure show template' do
      get "/adventures/#{adventure.last.id}"
      expect(response).to have_http_status(200)
      expect(response).to render_template :show
    end

    it 'does not save the new adventure' do
      expect{
        post '/adventures', adventure: { location: adventure.location,
                                     description: adventure.description }
      }.to_not change(adventure,:count)
    end

    it 're-renders the new page' do
      post '/adventures', adventure: { location: adventure.location,
                                   description: adventure.description }
      expect(response).to render_template :new
    end
  end

#------------------SHOW

  describe 'GET /adventures/:id' do
    before do
      post '/adventures', adventure: { date_time: adventure.date_time,
                                   activity: adventure.activity,
                                   location: adventure.location,
                                   description: adventure.description }
    end

    it "should render adventure show template" do
      get "/adventures/#{adventure.last.id}"
      expect(response).to have_http_status(200)
      expect(response).to render_template :show
    end
  end

#------------------EDIT/UPDATE

  describe 'GET /adventures/:id/edit' do
    it "should render adventures edit template" do
      get "/adventures/#{adventure.id}/edit"
      expect(response).to have_http_status(200)
      expect(response).to render_template :edit
    end
  end

  describe 'POST /adventures/:id' do
    before do
      post '/adventures', adventure: { date_time: adventure.date_time,
                                   activity: adventure.activity,
                                   location: adventure.location,
                                   description: adventure.description }
    end

    it "should update a adventure" do
      expect {
        patch "/adventures/#{adventure.id}", adventure: { date_time: adventure.date_time,
                                   activity: adventure.activity,
                                   location: adventure.location,
                                   description: adventure.description }
      }.to change(adventure, :count).by(0)
      expect(response).to have_http_status(302)
      expect(response).to redirect_to(adventure_url(adventure))
    end
  end

#-----EDIT/UPDATE FAILURE

  describe 'GET /adventures/:id/edit failure' do
    before do
      post '/adventures', adventure: { date_time: adventure.date_time,
                                   activity: adventure.activity,
                                   location: adventure.location,
                                   description: adventure.description }
    end

    it 're-renders the edit page' do
      get "/adventures/#{adventure.last.id}/edit", adventure: { date_time: adventure.date_time,
                                  activity: adventure.activity,
                                  location: adventure.location,
                                  description: adventure.description }
      expect(response).to render_template :edit
    end
  end

#------------------DELETE

  describe 'DELETE' do
    before do
      post '/adventures', adventure: { date_time: adventure.date_time,
                                   activity: adventure.activity,
                                   location: adventure.location,
                                   description: adventure.description }
    end

    it "should delete a adventure" do
      expect {
        delete "/adventures/#{adventure.last.id}"
      }.to change(adventure, :count).by(-1)
      expect(response).to have_http_status(302)
    end
  end

end
<file_sep>/app/controllers/adventures_controller.rb
class AdventuresController < ApplicationController
  before_action :set_adventure, only: [:show, :edit, :update, :destroy]

  before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
  before_action :owns_adventure, only: [:edit, :update, :destroy]
  before_action :prepare_categories
  # GET /adventures
  # GET /adventures.json
  def index
    @adventures = Adventure.all.order("updated_at DESC")

  #if params[:search]
  #  @adventures = Adventure.search(params[:search]).order("created_at DESC")
  #else
  #  @adventures = Adventure.all.order("created_at DESC")
  #end

  end

  def search
    if params[:search]
      @adventures = Adventure.search(params[:search]).order("created_at DESC")
    else
      @adventures = Adventure.all.order("created_at DESC")
    end
  end


  # GET /adventures/1
  # GET /adventures/1.json
  def show
    @adventure = Adventure.find(params[:id])
  end

  # GET /adventures/new
  def new
    @adventure = Adventure.new
    @adventure = current_user.adventures.build
  end

  # GET /adventures/1/edit
  def edit
  end

  # POST /adventures
  # POST /adventures.json
  def create
    @adventure = current_user.adventures.new(adventure_params)
    @adventure = Adventure.new(adventure_params)
    respond_to do |format|
      if @adventure.save
        format.html { redirect_to @adventure, notice: 'Adventure was successfully created.' }
        format.json { render :show, status: :created, location: @adventure }
      else
        format.html { render :new }
        format.json { render json: @adventure.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /adventures/1
  # PATCH/PUT /adventures/1.json
  def update
    respond_to do |format|
      if @adventure.update(adventure_params)
        format.html { redirect_to @adventure, notice: 'Adventure was successfully updated.' }
        format.json { render :show, status: :ok, location: @adventure }
      else
        format.html { render :edit }
        format.json { render json: @adventure.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /adventures/1
  # DELETE /adventures/1.json
  def destroy
    @adventure.destroy
    respond_to do |format|
      format.html { redirect_to adventures_url, notice: 'Adventure was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_adventure
      @adventure = Adventure.find(params[:id])
    end

    # add the @categories = Category.All to the before action so avail for all actions
     def prepare_categories
      @categories = Category.all
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def adventure_params
      params.require(:adventure).permit(:name, :description, :picture, :location, :visit, :address, :user_id, :category_id)
    end

    def owns_adventure
      if !user_signed_in? || current_user != Adventure.find(params[:id]).user
        redirect_to adventures_path, error: "You cannot do that"
      end
    end
end
<file_sep>/app/admin/adventure.rb
ActiveAdmin.register Adventure do
# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
# permit_params :list, :of, :attributes, :on, :model
#
# or
#
# permit_params do
#   permitted = [:permitted, :attributes]
#   permitted << :other if params[:action] == 'create' && current_user.admin?
#   permitted
# end
  permit_params :name, :description, :picture, :location, :visit, :address, :user_id, :category_id, :User

  index do
    column :name
    column :location
    column :user
    column :category
    actions
  end

end
<file_sep>/config/routes.rb
Rails.application.routes.draw do

  get 'contacts/new'
  resources :contacts, only: [:new, :create]

  devise_for :admin_users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)
  resources :categories
  devise_for :users
  resources :comments
  get 'pages/info'
  get 'pages/terms'
  get 'pages/privacy'




  root 'adventures#index'
  resources :adventures do
    collection do
      get :search
    end
  end
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
<file_sep>/app/models/adventure.rb
class Adventure < ApplicationRecord
  geocoded_by :address
  after_validation :geocode          # this command auto fetches our coordinates
  belongs_to :user
  belongs_to  :category
  mount_uploader :picture, PictureUploader

  def self.search(search)
  where("name ILIKE ? OR location ILIKE ? OR description ILIKE ?", "%#{search}%", "%#{search}%", "%#{search}%")
  end


end
<file_sep>/db/migrate/20170419022602_add_category_to_adventures.rb
class AddCategoryToAdventures < ActiveRecord::Migration[5.0]
  def change
    add_column :adventures, :category_id, :int
  end
end
<file_sep>/spec/factories/adventures.rb
FactoryBot.define do
  factory :adventure do
    date_time { Date.today.strftime("%A, %B %d %I:%M %P") }
    activity { FFaker::Sport.name }
    location { FFaker::AddressUS.neighborhood }
    description { FFaker::HipsterIpsum.words(4).join(',') }
  end
end
