Skip to content

Commit

Permalink
Added the search thing and algolia
Browse files Browse the repository at this point in the history
  • Loading branch information
rafxss committed Sep 15, 2020
1 parent d77f131 commit 172725e
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 12 deletions.
12 changes: 9 additions & 3 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
class ProductsController < ApplicationController

def index
@products = Product.all

if params[:query].present?
sql_query = "title ILIKE :query OR description ILIKE :query"
@products = Product.where(sql_query, query: "%#{params[:query]}%")
else
@products = Product.all
end
end

def new
Expand All @@ -18,7 +24,7 @@ def create
@product = Product.new(product_params)
@user = current_user
@product.user = @user

raise
if @product.save
redirect_to product_path(@product)

Expand All @@ -39,7 +45,7 @@ def update
private

def product_params
params.require(:product).permit(:name, :description, :location, :category, :price, photos: [])
params.require(:product).permit(:title, :description, :location, :category, :price, photos: [])

end
end
15 changes: 15 additions & 0 deletions app/javascript/controllers/location_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Controller } from "stimulus";
import places from "places.js";

export default class extends Controller {
connect() {
this.initAutocomplete();
}

initAutocomplete = () => {
const locationInput = document.getElementById("product_location");
if (locationInput) {
places({ container: locationInput })
}
};
}
2 changes: 1 addition & 1 deletion app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Product < ApplicationRecord
belongs_to :user
has_one :order

validates :description, :name, :category, :min_price, :location, presence: true
validates :description, :title, :category, :min_price, :location, presence: true
validates :username, uniqueness: true

has_many_attached :photos
Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ class User < ApplicationRecord

has_many :bought_products, source: :product, through: :orders

validates :username, uniqueness: true

has_one_attached :avatar
end
14 changes: 13 additions & 1 deletion app/views/devise/registrations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<p>Currently waiting confirmation for: <%= resource.unconfirmed_email %></p>
<% end %>

<%= f.input :username,
required: true,
autofocus: true,
input_html: { autocomplete: "username" }%>
<%= f.input :first_name,
required: false,
autofocus: true,
input_html: { autocomplete: "first_name" }%>
<%= f.input :last_name,
required: false,
autofocus: true,
input_html: { autocomplete: "last_name" }%>
<%= f.input :avatar, as: :file, input_html: {multiple: false}%>
<%= f.input :password,
hint: "leave it blank if you don't want to change it",
required: false,
Expand Down
4 changes: 4 additions & 0 deletions app/views/devise/registrations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<%= f.error_notification %>

<div class="form-inputs">
<%= f.input :username,
required: true,
autofocus: true,
input_html: { autocomplete: "username" }%>
<%= f.input :email,
required: true,
autofocus: true,
Expand Down
4 changes: 3 additions & 1 deletion app/views/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<div class="container">
<div data-controller="location"></div>
<%= simple_form_for(@product) do |f| %>
<%= f.input :name %>
<%= f.input :title %>
<%= f.input :description %>
<%= f.input :category %>
<%= f.input :location %>
<%= f.input :min_price, input_html: { min: '0' } %>
<%= f.input :photos, as: :file, input_html: { multiple: true } %>
<%= f.submit %>
<% end %>
</div>
5 changes: 5 additions & 0 deletions db/migrate/20200915141820_update_name_in_products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class UpdateNameInProducts < ActiveRecord::Migration[6.0]
def change
rename_column :products, :name, :title
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_09_15_124614) do
ActiveRecord::Schema.define(version: 2020_09_15_141820) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -37,7 +37,7 @@
end

create_table "products", force: :cascade do |t|
t.string "name"
t.string "title"
t.text "description"
t.string "category"
t.integer "min_price"
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"bootstrap": "^4.5.2",
"jquery": "^3.5.1",
"mapbox-gl": "^1.12.0",
"places.js": "^1.19.0",
"popper.js": "^1.16.1",
"stimulus": "^1.1.1",
"turbolinks": "^5.2.0"
Expand Down
Loading

0 comments on commit 172725e

Please sign in to comment.