Skip to content

refactor services index action #17

@jhackett1

Description

@jhackett1

this controller action is a bloody mess and could probably be improved.

    def index
        @services = Service.published.page(params[:page]).joins(:categories).preload(:categories).distinct
        if params[:postcode].present?
            @locations = Geocoder.search(params[:postcode], region: "gb")
            @services = @services.kinda_near(@locations.first.coordinates) if @locations.present?
            flash[:alert] = "Couldn't find any services near that location. Is it a valid postcode or area in Camden?" if @services.empty? || @locations.blank?
        end
        @services = @services.category(params[:category]) if params[:category].present?
        respond_to do |format|
            format.html
            format.json { render json: ServiceSerializer.new(@services).serializable_hash }
        end
    end

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions