Skip to content

Commit

Permalink
Add friendly_id
Browse files Browse the repository at this point in the history
  • Loading branch information
manhcuongdtbk committed Nov 6, 2018
1 parent 8679214 commit 8ed3053
Show file tree
Hide file tree
Showing 28 changed files with 368 additions and 137 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ gem "ckeditor"
gem "coffee-rails", "~> 4.2"
gem "devise"
gem "devise-i18n"
gem "friendly_id", "~> 5.2.0"
gem "jbuilder", "~> 2.5"
gem "jquery-rails"
gem "koala"
Expand Down
11 changes: 7 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GIT
remote: https://github.com/stympy/faker.git
revision: 723d7807516a425daead8f5e01e74b99515c0d1c
revision: a9f579d70aa907be7bf895f84cff186124bd9484
branch: master
specs:
faker (1.9.1)
Expand Down Expand Up @@ -75,7 +75,7 @@ GEM
builder (3.2.3)
byebug (10.0.2)
cancancan (2.3.0)
capybara (3.10.0)
capybara (3.10.1)
addressable
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
Expand Down Expand Up @@ -130,6 +130,8 @@ GEM
ffi (1.9.25)
font-awesome-rails (4.7.0.4)
railties (>= 3.2, < 6.0)
friendly_id (5.2.4)
activerecord (>= 4.0.0)
globalid (0.4.1)
activesupport (>= 4.2.0)
haml (5.0.4)
Expand Down Expand Up @@ -177,7 +179,7 @@ GEM
mini_mime (>= 0.1.1)
marcel (0.3.3)
mimemagic (~> 0.3.2)
method_source (0.9.0)
method_source (0.9.1)
mime-types (3.2.2)
mime-types-data (~> 3.2015)
mime-types-data (3.2018.0812)
Expand Down Expand Up @@ -317,7 +319,7 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
selenium-webdriver (3.14.1)
selenium-webdriver (3.141.0)
childprocess (~> 0.5)
rubyzip (~> 1.2, >= 1.2.2)
shoulda-matchers (4.0.0.rc1)
Expand Down Expand Up @@ -382,6 +384,7 @@ DEPENDENCIES
devise-i18n
factory_bot_rails
faker!
friendly_id (~> 5.2.0)
jbuilder (~> 2.5)
jquery-rails
koala
Expand Down
13 changes: 13 additions & 0 deletions app/assets/javascripts/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
$(document).ready(function () {
// Fadeout and autoclose alert
window.setTimeout(function() {
$(".alert").fadeTo(500, 0).slideUp(500, function(){
$(this).remove();
});
}, 1000);

// Datetimepicker
$('.datetimepicker').datetimepicker({
format: 'DD/MM/YYYY'
});
});
34 changes: 33 additions & 1 deletion app/assets/stylesheets/custom.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
/*# Pagination */
// Social share button

.social-share-button {
@extend .float-right ;
}

// Custom pagination

.pagination {
justify-content: center;
}

// Speeches-show

.main-raised.speeches-show {
margin: 80px 30px 0;
}

// Custom header

.navbar.navbar-transparent.custom-header {
background-color: #9c27b0 !important;
padding: 0.625rem 0;
margin-bottom: 0;
}

// Custom alert

#top-alert {
position: fixed;
top: 70px;
z-index: 69;
color: #fff;
background-color: #9c27b0;
width: 100%;
}
19 changes: 5 additions & 14 deletions app/controllers/speeches_controller.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
class SpeechesController < ApplicationController
load_and_authorize_resource param_method: :speech_params

def index
@pagy, @speeches = pagy Speech.all, items: 10
end
load_and_authorize_resource find_by: :slug

def show
@location = @speech.location
@theme = @speech.theme_ids
end

def new
@speech = Speech.new
end

def create
@speech.user = current_user
if @speech.save
flash[:success] = t ".success"
redirect_to @speech
Expand All @@ -23,9 +16,6 @@ def create
end
end

def edit
end

def update
if @speech.update_attributes speech_params
flash[:success] = t ".success"
Expand All @@ -38,11 +28,12 @@ def update
def destroy
@speech.destroy
flash[:success] = t ".success"
redirect_to speeches_path
redirect_to root_url
end

private
def speech_params
params.require(:speech).permit :title, :content, :speaking_day, :location_id, theme_ids: []
params.require(:speech).permit :title, :content, :speaking_day,
:location_id, theme_ids: []
end
end
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def show

private
def find_user
@user = User.find_by id: params[:id]
@user = User.friendly.find params[:id]
return if @user

flash[:info] = t ".info"
Expand Down
5 changes: 4 additions & 1 deletion app/models/speech.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
class Speech < ApplicationRecord
extend FriendlyId
friendly_id :title, use: :slugged

belongs_to :location
belongs_to :user
has_many :speech_themes
has_many :themes, through: :speech_themes

validates :title, :content, :speaking_day, :location_id, :user_id,
validates :title, :content, :speaking_day, :location_id, :user_id, :slug,
presence: true

scope :created_at_desc, ->{order created_at: :desc}
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
class User < ApplicationRecord
extend FriendlyId
friendly_id :name, use: :slugged

has_many :speeches, dependent: :destroy
has_many :services, dependent: :destroy
mount_uploader :avatar, AvatarUploader
validates :slug, presence: true

rolify
# Include default devise modules. Others available are:
Expand Down
1 change: 0 additions & 1 deletion app/views/devise/registrations/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<% provide :title, t(".title") %>
<% content_for :body_class, t(".body_class") %>
<% content_for :footer_class, t(".footer_class") %>

<div class="page-header header-filter" filter-color="purple" style="background: url(<%= asset_path 'bg7.jpg' %>); background-size: cover; background-position: top center;">
<div class="container">
Expand Down
1 change: 0 additions & 1 deletion app/views/devise/registrations/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<% provide :title, t(".title") %>
<% content_for :body_class, t(".body_class") %>
<% content_for :footer_class, t(".footer_class") %>

<div class="page-header header-filter" filter-color="purple" style="background: url(<%= asset_path 'bg7.jpg' %>); background-size: cover; background-position: top center;">
<div class="container">
Expand Down
1 change: 0 additions & 1 deletion app/views/devise/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<% provide :title, t(".title") %>
<% content_for :body_class, t(".body_class") %>
<% content_for :footer_class, t(".footer_class") %>

<div class="page-header header-filter" style="background: url(<%= asset_path 'bg7.jpg' %>); background-size: cover; background-position: top center;">
<div class="container">
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_footer.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<footer class="<%= yield(:footer_class) %>">
<footer class="footer">
<div class="container">
<nav class="pull-left">
<ul>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<nav class="navbar navbar-color-on-scroll navbar-transparent fixed-top navbar-expand-lg " color-on-scroll="100" id="sectionsNav">
<nav class="navbar navbar-color-on-scroll navbar-transparent fixed-top navbar-expand-lg <%= yield :custom_header %>" color-on-scroll="100" id="sectionsNav">
<div class="container">
<div class="navbar-translate">
<%= link_to t(".MSMS"), root_path, class: "navbar-brand" %>
Expand Down
14 changes: 12 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@
<body class="<%= yield :body_class %>">
<%= render "layouts/header" %>
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>"><%= message %></div>
<div class="alert alert-info" id="top-alert">
<div class="container">
<div class="alert-icon">
<i class="material-icons">info_outline</i>
</div>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true"><i class="material-icons">clear</i></span>
</button>
<b><%= t ".info" %></b> <%= message %>
</div>
</div>
<% end %>
<%= yield %>
<%= render "layouts/footer" %>
<%= render "layouts/comments_plugin" %>
<%= render "layouts/footer" %>
</body>
</html>
81 changes: 51 additions & 30 deletions app/views/speeches/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,36 +1,57 @@
<%= form_for @speech do |f| %>
<%= render "shared/error_messages", object: speech %>
<div class="main main-raised speeches-show">
<div class="container">
<div class="section section-text">
<div class="container">
<div class="row">
<div class="mx-auto mb-5 text-center">
<h1 class="title"><%= yield :title %></h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8 ml-auto mr-auto">
<%= form_for @speech do |f| %>
<%= render "shared/error_messages", object: speech %>

<div>
<%= f.label :themes %>
<ul class="book_form">
<%= f.collection_check_boxes :theme_ids, Theme.all, :id, :title do |theme| %>
<li class="theme_box">
<%= theme.label("data-value": theme.value) {theme.check_box + theme.text} %>
</li>
<% end %>
</ul>
</div>
<div>
<%= f.label :themes %>
<ul class="book_form list-unstyled">
<%= f.collection_check_boxes :theme_ids, Theme.all, :id, :title do |theme| %>
<li class="theme_box">
<%= theme.label("data-value": theme.value) {theme.check_box + theme.text} %>
</li>
<% end %>
</ul>
</div>

<div class="field">
<%= f.label :title %>
<%= f.text_field :title, placeholder: t(".title"), class: "form-control" %>
</div>
<div class="form-group has-default">
<%= f.label :title %>
<%= f.text_field :title, placeholder: t(".title"), class: "form-control" %>
</div>

<div class="field">
<%= f.label :content %>
<%= f.cktext_area :content, row: 20 %>
</div>
<div class="title">
<%= f.label :content %>
</div>
<div class="form-group label-floating">
<%= f.cktext_area :content, row: 5 %>
</div>

<div class="field">
<%= f.label :location %>
<%= f.select :location_id, options_from_collection_for_select(Location.all, :id, :name), class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :location %>
<%= f.select :location_id, options_from_collection_for_select(Location.all, :id, :name, @speech.location_id), {}, {class: "selectpicker mt-0", data: {style: "select-with-transition", size: "7"}} %>
</div>

<div class="field">
<%= f.label :speaking_day %>
<%= f.date_select :speaking_day, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :speaking_day, class: "label-control" %>
<%= f.text_field :speaking_day, class: "form-control datetimepicker", value: (@speech.speaking_day.blank? ? '' : @speech.speaking_day.strftime('%d/%m/%Y')) %>
</div>

<%= f.submit t(".save"), class: "btn btn-primary" %>
<% end %>
<div class="text-center">
<%= f.submit t(".save"), class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
</div>
</div>
</div>
10 changes: 4 additions & 6 deletions app/views/speeches/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<h1><%= t ".edit" %></h1>
<% provide :title, t(".title") %>
<% provide :custom_header, t(".custom_header") %>
<% content_for :body_class, t(".body_class") %>

<div>
<%= render "form", speech: @speech %>
</div>

<%= link_to t(".back"), speeches_path %>
<%= render "form", speech: @speech %>
6 changes: 0 additions & 6 deletions app/views/speeches/index.html.erb

This file was deleted.

9 changes: 4 additions & 5 deletions app/views/speeches/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<h1><%= t ".new" %></h1>
<% provide :title, t(".title") %>
<% provide :custom_header, t(".custom_header") %>
<% content_for :body_class, t(".body_class") %>

<div>
<%= render "form", speech: @speech %>
</div>
<%= link_to t(".back"), speeches_path %>
<%= render "form", speech: @speech %>
Loading

0 comments on commit 8ed3053

Please sign in to comment.