Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .hound.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
StringLiterals:
EnforcedStyle: single_quotes
100 changes: 31 additions & 69 deletions service_providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,40 @@ class ServiceProvidersController < ApplicationController

before_action :find_provider, only: [:show, :edit, :update, :destroy]
before_action :load_non_empty_cat, only: [:index, :landing] # fetch only categories with published companies in them
before_action :load_categories, only: [:new, :edit]
before_action :load_subcategories, only: [:new, :edit]
before_action :load_states, only: [:new, :edit]
before_action :load_all, only: [:new, :edit]

def landing
@providers = ServiceProvider.landing_sample

redirect_to service_providers_path if current_app_user
end

def index
@states = State.joins(:service_providers).uniq!
@state = State.find_by(name: params[:state])
@states = State.joins(:service_providers).uniq

if params[:search]

@providers = ServiceProvider.all_published.text_search(params[:search])

unless @providers.blank?
if @providers.present?
@nearby_providers = @providers.get_nearbys
end

# if a User clicks on a category:

elsif params[:category]
@category = @categories.find_by(id: params[:category])
@category = @categories.find(params[:category])
@providers = ServiceProvider.fetch_by_cat(@category, @state)
@show_title = @category.name

# if a User clicks on a subcategory:

elsif params[:subcategory]

@subcategory = Subcategory.find(params[:subcategory])
@providers = ServiceProvider.fetch_by_subcat(@subcategory, @state)
@show_title = @subcategory.name

elsif params[:state]
@state = State.find_by(name:params[:state])
@providers = @state.service_providers.all_published

else

@providers = ServiceProvider.all_published.includes(:city).includes(:state)

@providers = ServiceProvider.all_published.includes(:city, :state)
end

respond_to do |format|
Expand All @@ -61,7 +50,6 @@ def user_providers_index
@all_user_providers = ServiceProvider.user_providers(current_app_user)
@providers = @all_user_providers.where(published: true)
@unpublished_user_providers = @all_user_providers.where(published: false)

@link_share = LinkShare.new # creates a link_share
@sms_share = SmsShare.new # ~~~
end
Expand Down Expand Up @@ -90,7 +78,7 @@ def show
@printable = Printable.new
end

@subcategories = @provider.subcategories.order('category_id')
@subcategories = @provider.subcategories.order(:category_id)

respond_to do |format|
format.js
Expand All @@ -100,29 +88,26 @@ def show

def edit
if @provider.city
@cities = City.where(state_id: @provider.state).all
@cities = City.where(state_id: @provider.state)
end
end

def update

if @provider.update(service_provider_params)

send_to_list(params[:send_to_list], @provider)

redirect_to service_provider_path(@provider),
notice: "Service Provider updated successfully!"
flash[:notice] = 'Service Provider updated successfully!'
redirect_to service_provider_path(@provider)
else
@err = @provider.errors.full_messages
redirect_to edit_service_provider_path(@provider),
flash: { notice: "Invalid. Service provider not updated.", errors: @err}
flash[:notice] = 'Invalid. Service provider not updated.'
flash[:errors] = @err
redirect_to edit_service_provider_path(@provider)
end
end

def update_cities
# @provider = ServiceProvider.find_by(id: params[:provider_id])
@new_cities = City.where(state_id: params[:state_id]).all

# @provider = ServiceProvider.find(params[:provider_id])
@new_cities = City.where(state_id: params[:state_id])
render partial: 'service_providers/partials/cities', object: @new_cities
end

Expand All @@ -140,7 +125,7 @@ def destroy

def send_to_list(user_id, provider)
if user_id.present?
user = AppUser.find_by(id: user_id)
user = AppUser.find(user_id)
ListItem.create!(list: user.list, service_provider: provider)
end
end
Expand All @@ -150,24 +135,24 @@ def find_provider
end

def load_categories
@categories = Category.order('name asc').all
@categories = Category.order(:name)
end

def load_subcategories
@subcategories = Subcategory.order('name asc').all
@subcategories = Subcategory.order(:name)
end

def load_states
@states = State.all
end

def load_non_empty_cat
@categories = Category.with_published_providers.order('name asc').includes(:subcategories)
@categories = Category.with_published_providers.order(:name).
includes(:subcategories)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align the operands of an expression in an assignment spanning multiple lines.

end

def service_provider_params
if current_app_user.admin
params.require(:service_provider).
serv_params = params.require(:service_provider).
permit( :name,
:mission,
:online_tool,
Expand All @@ -184,7 +169,6 @@ def service_provider_params
:contact_email,
:phone,
:fax,
:published,
:cost,
:orgtype,
:facebook,
Expand All @@ -194,40 +178,18 @@ def service_provider_params
:state_id,
:city_id,
:bghex,
{:category_ids => []},
{:subcategory_ids => []}
)
else
params.require(:service_provider).
permit( :name,
:mission,
:online_tool,
:short_bio,
:image,
:street1,
:street2,
:city,
:state,
:zip_code,
:website,
:contact_person,
:gender,
:contact_email,
:phone,
:fax,
:cost,
:orgtype,
:facebook,
:instagram,
:twitter,
:youtube,
:state_id,
:city_id,
:bghex,
{:subcategory_ids => []},
{:category_ids => []}
{ subcategory_ids: [] },
{ category_ids: [] }
)
if current_app_user.admin
serv_params.merge!(params.require(:service_provider).permit(:published))
end
serv_params
end

end
def load_all
load_categories
load_subcategories
load_states
end
end