Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final version Lab 07 #84

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ git_source(:github) do |repo_name|
"https://github.com/#{repo_name}.git"
end


gem 'rails-i18n'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.4'
# Use sqlite3 as the database for Active Record
Expand Down
6 changes: 5 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ GEM
nokogiri (>= 1.6)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
rails-i18n (5.0.4)
i18n (~> 0.7)
railties (~> 5.0)
railties (5.1.4)
actionpack (= 5.1.4)
activesupport (= 5.1.4)
Expand Down Expand Up @@ -182,6 +185,7 @@ DEPENDENCIES
listen (>= 3.0.5, < 3.2)
puma (~> 3.7)
rails (~> 5.1.4)
rails-i18n
sass-rails (~> 5.0)
selenium-webdriver
spring
Expand All @@ -193,4 +197,4 @@ DEPENDENCIES
web-console (>= 3.3.0)

BUNDLED WITH
1.16.0
1.16.0
4 changes: 4 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def show
def new
@comment = Comment.new
end

def button
@comment = Comment.addLike
end

# GET /comments/1/edit
def edit
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/pumpkins_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ class PumpkinsController < ApplicationController
# GET /pumpkins.json
def index
@pumpkins = Pumpkin.all
if params["pumpkinName"]
Pumpkin.all.searchPumpkin(params["pumpkinName"])
if params[:carvedOrNot]
@pumpkins = Pumpkin.searchForCarved(params[:carvedOrNot])
else
@pumpkins = Pumpkin.all
end

@show = t '.show'
Expand Down
4 changes: 4 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class Comment < ApplicationRecord
belongs_to :pumpkin

def self.addLike
self[:likes] += 1
end
end
14 changes: 11 additions & 3 deletions app/models/pumpkin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ class Pumpkin < ApplicationRecord
has_many :orders, through: :line_items
has_many :comments, dependent: :destroy

def searchPumpkin (p)
Pumpkin.find_by_sql("SELECT * FROM pumpkins
WHERE name = p")
def self.searchForCarved(search)
if search != ""
if search == "true"
search = true
else
search = false
end
where("carved LIKE ?", search)
else
where("carved LIKE ? OR carved LIKE ?", true, false)
end
end

end
1 change: 1 addition & 0 deletions app/views/comments/button.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Hi</h1>
7 changes: 7 additions & 0 deletions app/views/comments/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,12 @@
<%= @comment.pumpkin %>
</p>

<p>
<strong>Likes:</strong>
<%= @comment.likes %>
</p>

<%= link_to 'Add like', button_path %>

<%= link_to 'Edit', edit_comment_path(@comment) %> |
<%= link_to 'Back', comments_path %>
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<%= link_to '[EN]', locale: 'en' %>
<%= link_to '[DE]', locale: 'de' %>
<%= link_to '[NO]', locale: 'nn' %>
<%= yield %>
</body>
</html>
5 changes: 2 additions & 3 deletions app/views/pumpkins/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ locale: <%= params[:locale] %>
<%= link_to @new, new_pumpkin_path %>


<%= form_tag("/pumpkins", method: "get") do %>
<%= label_tag(:pumpkinName, @search_for) %>
<%= text_field_tag(:pumpkinName) %>
<%= form_tag(pumpkins_path, method: "get") do %>
<%= text_field_tag :carvedOrNot , params[:carvedOrNot], placeholder: "Carved true or false?" %>
<%= submit_tag(t '.search') %>
<% end %>
2 changes: 1 addition & 1 deletion config/initializers/locale.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
I18n.available_locales = [:en,:de]
I18n.available_locales = [:en,:de,:nn]
I18n.default_locale = :de
26 changes: 13 additions & 13 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ de:
hello: "Hallo Welt"
layouts:
application:
app_name: Die HTW Anwendung
app_name: "Die HTW Anwendung"
pumpkins:
index:
pumpkins: Kürbisse
species: Spezies
carved: Verziert
size: Größe
weight: Gewicht
price: Preis
show: Anzeigen
edit: Bearbeiten
delete: Löschen
new: Neuer Kürbis
search: Suchen
search_for: Suche nach
pumpkins: "Kürbisse"
species: "Spezies"
carved: "Verziert"
size: "Größe"
weight: "Gewicht"
price: "Preis"
show: "Anzeigen"
edit: "Bearbeiten"
delete: "Löschen"
new: "Neuer Kürbis"
search: "Suchen"
search_for: "Suche nach"
19 changes: 19 additions & 0 deletions config/locales/nn.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
nn:
hello: "Hei verden"
layouts:
application:
app_name: "Min HTW applikasjon"
pumpkins:
index:
pumpkins: "Gresskar"
species: "Art"
carved: "Pyntet"
size: "Størrelse"
weight: "Vekt"
price: "Pris"
show: "Vis"
edit: "Rediger"
delete: "Slett"
new: "Ny gresskar"
search: "Søk"
search_for: "Søk etter"
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
scope '(:locale)' do
resources :orders
resources :customers
resources :pumpkins
resources :pumpkins
root to: 'startpage#welcome'
# For details on the DSL available
# within this file,
# see http://guides.rubyonrails.org/routing.html
end
get '/comments/:id', to: "comments#button", as: "button"
end
5 changes: 5 additions & 0 deletions db/migrate/20180205154852_add_likes_to_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddLikesToComments < ActiveRecord::Migration[5.1]
def change
add_column :comments, :likes, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180116131334) do
ActiveRecord::Schema.define(version: 20180205154852) do

create_table "comments", force: :cascade do |t|
t.string "title"
t.text "content"
t.integer "pumpkin_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "likes"
t.index ["pumpkin_id"], name: "index_comments_on_pumpkin_id"
end

Expand Down
4 changes: 2 additions & 2 deletions db/seeds/pumpkin_with_comments.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
p1 = Pumpkin.create(species: 'Pumpkin with comments', carved: false, size: 5, weight: 7, price: 13)
p1.comments.create(title:"Best Pumpkin", content: "This is a really delicious pumpkin")
p1.comments.create(title:"Hard to carve", content: "The shape makes it difficult to carve for Halloween")
p1.comments.create(title:"Best Pumpkin", content: "This is a really delicious pumpkin", likes: 15)
p1.comments.create(title:"Hard to carve", content: "The shape makes it difficult to carve for Halloween", likes: 0)