Skip to content

Commit f8e0e6e

Browse files
authored
Tutorials CRUD (#590)
* Add new tutorials path to navs and remove old 'help' page * Change helper text re client artwork * Add new_attributes for various specs that were being skipped * Update scaffold templates for index and controller * Tutorials CRUD * Fix spec showing before and after login * Don't skip before_action on dashboard for :help anymore * Correctly redirect to index
1 parent bdb559f commit f8e0e6e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+943
-365
lines changed

app/controllers/dashboard_controller.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class DashboardController < ApplicationController
2-
skip_before_action :authenticate_user!, only: :help
32
include AdminDashboardCardsHelper
43

54
def index
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class TutorialsController < ApplicationController
2+
before_action :set_tutorial, only: [:show, :edit, :update, :destroy]
3+
4+
def index
5+
per_page = params[:number_of_items_per_page].presence || 25
6+
unfiltered = current_user.super_user? ? Tutorial.all : Tutorial.published
7+
filtered = unfiltered.search_by_params(params)
8+
@count_display = filtered.count == unfiltered.count ? unfiltered.count : "#{filtered.count}/#{unfiltered.count}"
9+
@tutorials = filtered.order(:position).paginate(page: params[:page], per_page: per_page).decorate
10+
end
11+
12+
def show
13+
@tutorial = @tutorial.decorate
14+
end
15+
16+
def new
17+
@tutorial = Tutorial.new.decorate
18+
set_form_variables
19+
end
20+
21+
def edit
22+
@tutorial = @tutorial.decorate
23+
set_form_variables
24+
end
25+
26+
def create
27+
@tutorial = Tutorial.new(tutorial_params)
28+
29+
if @tutorial.save
30+
redirect_to tutorials_path, notice: "Tutorial was successfully created."
31+
else
32+
@tutorial = @tutorial.decorate
33+
set_form_variables
34+
render :new, status: :unprocessable_content
35+
end
36+
end
37+
38+
def update
39+
if @tutorial.update(tutorial_params)
40+
redirect_to tutorials_path, notice: "Tutorial was successfully updated.", status: :see_other
41+
else
42+
@tutorial = @tutorial.decorate
43+
set_form_variables
44+
render :edit, status: :unprocessable_content
45+
end
46+
end
47+
48+
def destroy
49+
@tutorial.destroy!
50+
redirect_to tutorials_path, notice: "Tutorial was successfully destroyed."
51+
end
52+
53+
# Optional hooks for setting variables for forms or index
54+
def set_form_variables
55+
@tutorial.build_main_image if @tutorial.main_image.blank?
56+
@tutorial.gallery_images.build
57+
end
58+
59+
private
60+
61+
def set_tutorial
62+
@tutorial = Tutorial.find(params[:id])
63+
end
64+
65+
# Strong parameters
66+
def tutorial_params
67+
params.require(:tutorial).permit(
68+
:title, :body, :rhino_body, :featured, :published, :position, :youtube_url,
69+
main_image_attributes: [:id, :file, :_destroy],
70+
gallery_images_attributes: [:id, :file, :_destroy],
71+
)
72+
end
73+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class TutorialDecorator < Draper::Decorator
2+
delegate_all
3+
4+
def display_text
5+
"<div class='reset-list-items'>#{body}</div>".html_safe
6+
end
7+
8+
end

app/helpers/youtube_helper.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module YoutubeHelper
2+
def youtube_embed_url(url)
3+
return if url.blank?
4+
5+
uri = URI.parse(url) rescue nil
6+
return unless uri
7+
8+
if uri.host&.include?("youtu.be")
9+
video_id = uri.path.delete_prefix("/")
10+
elsif uri.query
11+
params = Rack::Utils.parse_query(uri.query)
12+
video_id = params["v"]
13+
end
14+
15+
return unless video_id
16+
17+
"https://www.youtube.com/embed/#{video_id}"
18+
end
19+
end

app/models/tutorial.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Tutorial < ApplicationRecord
2+
include TagFilterable
3+
4+
has_rich_text :rhino_body
5+
6+
has_many :bookmarks, as: :bookmarkable, dependent: :destroy
7+
# Image associations
8+
has_one :main_image, -> { where(type: "Images::MainImage") },
9+
as: :owner, class_name: "Images::MainImage", dependent: :destroy
10+
has_many :gallery_images, -> { where(type: "Images::GalleryImage") },
11+
as: :owner, class_name: "Images::GalleryImage", dependent: :destroy
12+
13+
validates :title, presence: true, uniqueness: { case_sensitive: false }
14+
15+
# Nested attributes
16+
accepts_nested_attributes_for :main_image, reject_if: :all_blank, allow_destroy: true
17+
accepts_nested_attributes_for :gallery_images, reject_if: :all_blank, allow_destroy: true
18+
19+
# SearchCop
20+
include SearchCop
21+
search_scope :search do
22+
attributes :title, :body
23+
end
24+
25+
scope :body, -> (body) { where("body like ?", "%#{ body }%") }
26+
scope :published, ->(published=nil) {
27+
if ["true", "false"].include?(published)
28+
where(published: published)
29+
else
30+
where(published: true)
31+
end
32+
}
33+
scope :published_search, -> (published_search) { published_search.present? ? published(published_search) : all }
34+
scope :title, -> (title) { where("title like ?", "%#{ title }%") }
35+
scope :tutorial_name, -> (tutorial_name) { title(tutorial_name) }
36+
37+
def self.search_by_params(params)
38+
resources = all
39+
resources = resources.search(params[:search]) if params[:search].present?
40+
resources = resources.title(params[:title]) if params[:title].present?
41+
resources = resources.body(params[:body]) if params[:body].present?
42+
resources = resources.published(params[:published]) if params[:published].present?
43+
resources
44+
end
45+
end

0 commit comments

Comments
 (0)