working form

This commit is contained in:
Senad Uka
2016-09-08 09:59:42 +02:00
parent ec1926dec6
commit 50df27b0c0
60 changed files with 76113 additions and 46 deletions

View File

@@ -13,3 +13,5 @@
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require unpoly
//= require bootstrap-sprockets

View File

@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View File

@@ -1,16 +0,0 @@
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope.
*
*= require twitter/bootstrap
*= require_tree .
*= require_self
*/

View File

@@ -0,0 +1,8 @@
/*
**** require_self
*/
// "bootstrap-sprockets" must be imported before "bootstrap" and "bootstrap/variables"
@import "bootstrap-sprockets";
@import "bootstrap";
@import "bootstrap/theme";
@import "proverbs";

View File

@@ -0,0 +1,3 @@
// Place all the styles related to the Authors controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View File

@@ -0,0 +1,7 @@
// Place all the styles related to the Proverbs controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
.hamo {
display: block;
}

View File

@@ -0,0 +1,3 @@
// Place all the styles related to the Users controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View File

@@ -1,3 +1,6 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
layout 'application'
end

View File

@@ -0,0 +1,2 @@
class AuthorsController < ApplicationController
end

View File

@@ -0,0 +1,68 @@
class ProverbsController < ApplicationController
before_action :set_proverb, only: [:show, :destroy]
# GET /proverbs/1
# GET /proverbs/1.json
def show
end
# GET /proverbs/new
def new
@proverb = Proverb.new
end
# POST /proverbs
# POST /proverbs.json
def create
@proverb = Proverb.new(proverb_params)
respond_to do |format|
if @proverb.save
format.html { redirect_to @proverb, notice: 'Proverb was successfully created.' }
format.json { render :show, status: :created, location: @proverb }
else
format.html { render :new }
format.json { render json: @proverb.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /proverbs/1
# PATCH/PUT /proverbs/1.json
def update
respond_to do |format|
if @proverb.update(proverb_params)
format.html { redirect_to @proverb, notice: 'Proverb was successfully updated.' }
format.json { render :show, status: :ok, location: @proverb }
else
format.html { render :edit }
format.json { render json: @proverb.errors, status: :unprocessable_entity }
end
end
end
# GET /proverbs/random
# GET /proverbs/random.json
def random
offset = rand(Proverb.count)
proverb = Proverb.offset(offset).first
if proverb
redirect_to proverb_path(proverb)
else
redirect_to new_proverb_path
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_proverb
@proverb = Proverb.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def proverb_params
params.require(:proverb).permit(:quote, :author_name, :user_email)
end
end

View File

@@ -0,0 +1,2 @@
class UsersController < ApplicationController
end

View File

@@ -0,0 +1,2 @@
module AuthorsHelper
end

View File

@@ -0,0 +1,2 @@
module ProverbsHelper
end

View File

@@ -0,0 +1,2 @@
module UsersHelper
end

11
app/models/author.rb Normal file
View File

@@ -0,0 +1,11 @@
class Author < ApplicationRecord
has_many :proverbs
validates :name, presence: true
validates :unique_id, presence: true, uniqueness: true
def identify_uniquely
self.unique_id = name.to_s.downcase.gsub(/[\W\s]/,'')
end
end

40
app/models/proverb.rb Normal file
View File

@@ -0,0 +1,40 @@
class Proverb < ApplicationRecord
belongs_to :author
belongs_to :user
before_validation :setup_ownership
attr_writer :author_name, :user_email
def author_name
author.try(:name) || @author_name
end
def user_email
user.try(:email) || @user_email
end
private
def setup_ownership
if author
author.update_attributes(name: author_name)
else
self.author = Author.new(name: author_name)
author.identify_uniquely
author.save
end
if user
user.update_attributes(email: user_email.downcase)
else
self.user = User.create(email: user_email.downcase)
end
end
def attributes
super.merge(author_name: author_name, user_email: user_email)
end
end

6
app/models/user.rb Normal file
View File

@@ -0,0 +1,6 @@
class User < ApplicationRecord
has_many :proverbs
validates :email, presence: true, uniqueness: true
end

View File

@@ -1,14 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<title>Izreke</title>
<%= csrf_meta_tags %>
<head>
<title>Izreke</title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
</head>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
</head>
<body>
<%= yield %>
</body>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header text-center">
<%= link_to random_proverbs_path, class: "navbar-brand" do %>
Izreke
<% end %>
</div>
</nav>
</div>
</div>
<%= yield %>
</div>
</body>
</html>

View File

@@ -0,0 +1,37 @@
<%= form_for(proverb, class: 'form') do |f| %>
<div class="row">
<div class="col-xs-8 col-xs-offset-2 text-center">
<% if proverb.errors.any? %>
<div id="error_explanation">
<h2>Greška:
</h2>
<ul>
<% proverb.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<div class="field">
<%= f.text_field :author_name, class: 'form-control', placeholder: 'Ime autora izreke', required: true %>
</div>
<div class="field">
<%= f.email_field :user_email, class: 'form-control', placeholder: 'Vaša Email Adresa', required: true %>
</div>
<div class="field">
<%= label_tag "Izreka" %>
<%= f.text_area :quote, :rows => 5, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.submit "Dodaj izreku!" , class: 'btn btn_large btn-success' %>
</div>
</div>
<% end %>
</div>
</div>

View File

@@ -0,0 +1,2 @@
json.extract! proverb, :id, :quote, :author_id, :user_id, :created_at, :updated_at
json.url proverb_url(proverb, format: :json)

View File

@@ -0,0 +1,2 @@
<%= render 'form', proverb: @proverb %>

View File

@@ -0,0 +1,25 @@
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-md-12">
<div class="jumbotron text-center">
<blockquote>
<p>
<%= @proverb.quote %>
</p>
<footer><%= @proverb.author.try(:name)%>
</footer>
</blockquote>
<p>
<%= link_to random_proverbs_path, class: "btn btn-primary btn-large" do %>
Sljedeća
<% end %>
<%= link_to new_proverb_path, class: "btn btn-success btn-large" do %>
Nova
<% end %>
</p>
</div>
</div>
</div>

View File

@@ -0,0 +1 @@
json.partial! "proverbs/proverb", proverb: @proverb