-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Speaker module for Admin (#182)
* Create Factory for events * Generate Speaker module for admin - Add HTTParty to validate links - Add views, controllers, policies, model for speaker * Config custom error view to adapt to bootstrap * Fix lint errors
- Loading branch information
1 parent
3a96127
commit b96e0ca
Showing
31 changed files
with
705 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,11 @@ module.exports = { | |
}, | ||
], | ||
}, | ||
settings: { | ||
react: { | ||
version: 'detect', | ||
}, | ||
}, | ||
overrides: [ | ||
{ | ||
files: [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# frozen_string_literal: true | ||
module Admin | ||
class SpeakersController < AdminController | ||
before_action :authorize_event | ||
before_action :set_speaker, only: %w[show edit update destroy] | ||
|
||
# GET /admin/speakers | ||
def index | ||
@speakers = Speaker.ordered_by_name | ||
end | ||
|
||
# GET /admin/speakers/1 | ||
def show; end | ||
|
||
# GET /admin/speakers/new | ||
def new | ||
@speaker = Speaker.new | ||
end | ||
|
||
# GET /admin/speakers/1/edit | ||
def edit | ||
render_not_found unless @speaker | ||
end | ||
|
||
# POST /admin/speakers | ||
def create | ||
@speaker = Speaker.new(speaker_params) | ||
|
||
respond_to do |format| | ||
if @speaker.save | ||
format.html do | ||
redirect_to edit_admin_speaker_url(@speaker), | ||
notice: 'Speaker was successfully created.' | ||
end | ||
else | ||
format.html { render :new, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PATCH/PUT /admin/speakers/1 | ||
def update | ||
respond_to do |format| | ||
if @speaker.update(speaker_params) | ||
format.html do | ||
redirect_to edit_admin_speaker_url(@speaker), | ||
notice: 'Speaker was successfully updated.' | ||
end | ||
format.json { render :show, status: :ok, location: @speaker } | ||
else | ||
format.html { render :edit, status: :unprocessable_entity } | ||
format.json { render json: @speaker.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /admin/speakers/1 | ||
# def destroy | ||
# @speaker.destroy | ||
|
||
# respond_to do |format| | ||
# format.html do | ||
# redirect_to admin_speakers_url, notice: 'Speaker was successfully destroyed.' | ||
# end | ||
# end | ||
# end | ||
|
||
private | ||
|
||
def authorize_event | ||
authorize Event | ||
end | ||
|
||
# Use callbacks to share common setup or constraints between actions. | ||
def set_speaker | ||
@speaker = Speaker.find(params[:id]) | ||
end | ||
|
||
# Only allow a list of trusted parameters through. | ||
def speaker_params | ||
params.require(:speaker).permit( | ||
:name, | ||
:bio, | ||
:tagline, | ||
:image_url, | ||
:github, | ||
:linkedin, | ||
:mastodon, | ||
:website, | ||
:twitter, | ||
:other, | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
module Admin | ||
module SpeakersHelper | ||
#Add helpers | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
class Speaker < ApplicationRecord | ||
include HTTParty | ||
SOCIAL_MEDIA_LINKS = %w[github linkedin mastodon twitter website other].freeze | ||
|
||
has_many :event_speakers, dependent: :destroy | ||
has_many :events, through: :event_speakers | ||
|
||
validates :name, :bio, :image_url, presence: true | ||
validate :links, :validate_social_media_brand | ||
validate :url_exists?, if: ->(s) { s.links.compact_blank.present? } | ||
|
||
store :links, accessors: SOCIAL_MEDIA_LINKS, coder: JSON | ||
|
||
before_validation :format_links | ||
|
||
scope :ordered_by_name, -> { order(:name) } | ||
|
||
def validate_social_media_brand | ||
return if links.blank? || links.keys.all? { |key| SOCIAL_MEDIA_LINKS.include?(key) } | ||
|
||
errors.add(:links, 'This social media is not allowed') | ||
end | ||
|
||
def format_links | ||
return if links.blank? | ||
|
||
links.transform_keys!(&:downcase) | ||
end | ||
|
||
def url_exists? | ||
links.each { |_, url| errors.add(:links, 'This url is not valid') unless url_valid?(url) } | ||
end | ||
|
||
def empty_links? | ||
links.compact_blank.empty? | ||
end | ||
|
||
private | ||
|
||
def url_valid?(url) | ||
response = HTTParty.get(url) | ||
response.code == 200 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
class SpeakerPolicy < ApplicationPolicy | ||
def index? | ||
user.admin? | ||
end | ||
|
||
def new? | ||
user.admin? | ||
end | ||
|
||
def create? | ||
user.admin? | ||
end | ||
|
||
def edit? | ||
user.admin? | ||
end | ||
|
||
def show? | ||
edit? | ||
end | ||
|
||
def update? | ||
user.admin? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<div class="row"> | ||
<div class="col col-lg-6"> | ||
<h1>Edit event</h1> | ||
<%=render 'form'%> | ||
<%=render 'form', event: @event %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,45 @@ | ||
<h1 class="mb-4">Event List</h1> | ||
<div class="row"> | ||
<div class="col"> | ||
<%= link_to new_admin_event_path, class: "btn btn-primary btn-lg mb-3" do %> | ||
<%= link_to new_admin_event_path, class: "btn btn-primary btn-md mb-4" do %> | ||
Create new Event | ||
<i class="bi bi-plus fw-semibold"></i> | ||
<% end %> | ||
</div> | ||
</div> | ||
<div class="row"> | ||
<div class="col"> | ||
<table class="table text-center"> | ||
<thead class="border-2"> | ||
<tr> | ||
<th scope="col" class="p-3">Title</th> | ||
<th scope="col" class="p-3">Type</th> | ||
<th scope="col" class="p-3">Speakers</th> | ||
<th scope="col" class="p-3">Date</th> | ||
<th scope="col" class="p-3">Location</th> | ||
<th scope="col" class="p-3">Description</th> | ||
<th scope="col" class="p-3">Actions</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% @events.each do |event| %> | ||
<tr class="border-2"> | ||
<td class="p-3"><%= event.title %></td> | ||
<td class="p-3"><%= event.type %></td> | ||
<td class="p-3"><%= event.speakers.map(&:name).join(", ") %></td> | ||
<td class="p-3"><%= event.date.strftime('%B %d, %Y %I:%M%p %:z') %></td> | ||
<td class="p-3"><%= event.location %></td> | ||
<td class="p-3"><%= truncate_html(event.description) %></td> | ||
<td class="p-3"> | ||
<%= link_to 'Edit', edit_admin_event_path(event) %> | ||
<%= link_to 'Delete', admin_event_path(event), method: :delete %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
<div class="table-responsive"> | ||
<table class="table table-sm text-center table-hover"> | ||
<thead class="border-2"> | ||
<tr class="table-primary"> | ||
<th scope="col" class="p-3">Title</th> | ||
<th scope="col" class="p-3">Type</th> | ||
<th scope="col" class="p-3">Speakers</th> | ||
<th scope="col" class="p-3">Date</th> | ||
<th scope="col" class="p-3">Location</th> | ||
<th scope="col" class="p-3">Description</th> | ||
<th scope="col" class="p-3">Actions</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% @events.each do |event| %> | ||
|
||
<tr class="border-2"> | ||
<td class="p-3"><%= event.title %></td> | ||
<td class="p-3"><%= event.type %></td> | ||
<td class="p-3"><%= event.speakers.map(&:name).join(", ") %></td> | ||
<td class="p-3"><%= event.date.strftime('%B %d, %Y %I:%M%p %:z') %></td> | ||
<td class="p-3"><%= event.location %></td> | ||
<td class="p-3"><%= truncate_html(event.description) %></td> | ||
<td class="p-3"> | ||
<%= link_to 'Edit', edit_admin_event_path(event) %> | ||
<%= button_to "Delete", admin_event_url(event), form_class: "", class: "link-primary border-0 bg-transparent text-decoration-underline", method: :delete %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<div class="row"> | ||
<div class="col col-lg-6"> | ||
<h1>New Event</h1> | ||
<%= render 'form'%> | ||
<%= render 'form', event: @event %> | ||
</div> | ||
</div> |
Oops, something went wrong.