timelines#index

This commit is contained in:
Andrew Brown 2020-06-10 15:04:51 -05:00
parent ed390d4f71
commit 438a7b4eef
14 changed files with 364 additions and 12 deletions

View File

@ -0,0 +1,101 @@
class TimelinesController < ApplicationController
before_action :set_timeline, only: [:show, :edit, :update, :destroy]
before_action :set_navbar_color
before_action :set_navbar_actions
before_action :set_sidenav_expansion
# GET /timelines
def index
@timelines = current_user.timelines
end
# GET /timelines/1
def show
end
# GET /timelines/new
def new
@timeline = Timeline.new
end
# GET /timelines/1/edit
def edit
end
# POST /timelines
def create
@timeline = Timeline.new(timeline_params)
if @timeline.save
redirect_to @timeline, notice: 'Timeline was successfully created.'
else
render :new
end
end
# PATCH/PUT /timelines/1
def update
if @timeline.update(timeline_params)
redirect_to @timeline, notice: 'Timeline was successfully updated.'
else
render :edit
end
end
# DELETE /timelines/1
def destroy
@timeline.destroy
redirect_to timelines_url, notice: 'Timeline was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_timeline
@timeline = Timeline.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def timeline_params
params.fetch(:timeline, {})
end
def set_navbar_color
@navbar_color = Timeline.hex_color.presence || '#2196F3'
end
def set_navbar_actions
# content_type = @content_type_class || content_type_from_controller(self.class)
# @navbar_actions = []
# return if [AttributeCategory, AttributeField].include?(content_type)
# if user_signed_in?
# if @current_user_content
# @navbar_actions << {
# label: "Your #{view_context.pluralize @current_user_content.fetch(content_type.name, []).count, content_type.name.downcase}",
# href: main_app.polymorphic_path(content_type)
# }
# end
# @navbar_actions << {
# label: "New #{content_type.name.downcase}",
# href: main_app.new_polymorphic_path(content_type),
# class: 'right'
# }
# end
# discussions_link = ForumsLinkbuilderService.worldbuilding_url(content_type)
# if discussions_link.present?
# @navbar_actions << {
# label: 'Discussions',
# href: discussions_link
# }
# end
end
def set_sidenav_expansion
@sidenav_expansion = 'writing'
end
end

View File

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

View File

@ -10,6 +10,7 @@ module HasContent
#has_many :characters, :locations, etc
has_many content_type_sym, dependent: :destroy
end
has_many :timelines
has_many :attribute_fields
has_many :attribute_categories

View File

@ -15,7 +15,7 @@ class Universe < ApplicationRecord
include Authority::Abilities
self.authorizer_name = 'UniverseCoreContentAuthorizer'
validates :name, presence: true
validates :name, presence: true
validates :user_id, presence: true
belongs_to :user

View File

@ -1,4 +1,32 @@
class Timeline < ApplicationRecord
acts_as_paranoid
include IsContentPage
include Authority::Abilities
self.authorizer_name = 'ExtendedContentAuthorizer'
belongs_to :universe
belongs_to :user
validates :user_id, presence: true
def self.content_name
'timeline'
end
def description
'A timeline'
end
def self.color
'green'
end
def self.hex_color
'#4CAF50'
end
def self.icon
'timeline'
end
end

View File

@ -96,12 +96,6 @@
</a>
<div class="collapsible-body" style="">
<ul>
<li>
<%= link_to main_app.prompts_path, class: 'waves-effect' do %>
<i class="material-icons left orange-text">lightbulb_outline</i>
Prompts
<% end %>
</li>
<li>
<%= link_to main_app.documents_path, class: 'waves-effect' do %>
<i class="material-icons <%= Document.color %>-text">
@ -109,10 +103,25 @@
</i>
Documents
<span class="badge">
<%= @current_user_content['Document'].count %>
<%= @current_user_content.fetch('Document', []).count %>
</span>
<% end %>
</li>
<li>
<%= link_to main_app.timelines_path, class: 'waves-effect' do %>
<i class="material-icons left <%= Timeline.color %>-text"><%= Timeline.icon %></i>
Timelines
<span class="badge">
<%= @current_user_content.fetch('Timeline', []).count %>
</span>
<% end %>
</li>
<li>
<%= link_to main_app.prompts_path, class: 'waves-effect' do %>
<i class="material-icons left orange-text">lightbulb_outline</i>
Prompts
<% end %>
</li>
</ul>
</div>
</li>

View File

@ -0,0 +1,17 @@
<%= form_with(model: timeline, local: true) do |form| %>
<% if timeline.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(timeline.errors.count, "error") %> prohibited this timeline from being saved:</h2>
<ul>
<% timeline.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="actions">
<%= form.submit %>
</div>
<% end %>

View File

@ -0,0 +1,6 @@
<h1>Editing Timeline</h1>
<%= render 'form', timeline: @timeline %>
<%= link_to 'Show', @timeline %> |
<%= link_to 'Back', timelines_path %>

View File

@ -0,0 +1,92 @@
<%= content_for :full_width_page_header do %>
<%= render partial: 'content/components/parallax_header', locals: { content_type: 'Timeline', content_class: Timeline } %>
<% end %>
<% if @timelines.any? %>
<div class="row">
<div class="col s12">
<%# render partial: 'content/components/list_filter_bar', locals: { content_type: Timeline } %>
</div>
<%# render partial: 'content/list/cards', locals: { content_list: @content, content_type: @content_type_class, show_add_another_form: true, show_template_editor_form: true } %>
</div>
<% if @show_scope_notice %>
<p class="center help-text">
Only showing timelines
in the <%= link_to @universe_scope.name, @universe_scope, class: Universe.color + '-text' %> universe.
<%= link_to(
"See timelines from all universes.",
'?universe=all',
class: Timeline.color + '-text')
%>
</p>
<% end %>
<% elsif @timelines.empty? %>
<div class="row">
<% if @current_user_content.fetch('Timeline', []).count > 0 %>
<div class="col s12 center">
<%# render partial: 'content/components/list_filter_bar', locals: { content_type: Timeline } %>
</div>
<% end %>
<div class="col s12 m8 offset-m2">
<div class="hoverable card center" style="margin: 20px 0 0 0; padding: 50px 0; border-bottom: 10px solid <%= Timeline.hex_color %>">
<h4>
You haven't created any timelines
<% if @universe_scope %>
in the <%= link_to @universe_scope.name, @universe_scope %> universe
<% end %>
<% if params.key?(:favorite_only) || params.key?(:slug) %>
that match these filters
<% end %>
yet!
</h4>
<h1>
<i class="material-icons <%= Timeline.color %>-text" style="font-size: 200%">
<%= Timeline.icon %>
</i>
</h1>
<p>
<%= t("content_descriptions.timelines") %>
</p>
<% if current_user.can_create?(Timeline) || PermissionService.user_has_active_promotion_for_this_content_type(user: current_user, content_type: 'Timeline') %>
<%= link_to "Create your first timeline", new_timeline_path(), class: "btn-large #{Timeline.color} darken-1" %>
<% else %>
<%= link_to "Upgrade to Premium to create your first timeline", subscription_path %>
<% end %>
<p class="grey-text text-lighten-2" style="padding-top: 20px;">
<% 5.times { %><i class="material-icons"><%= Timeline.icon %></i>&nbsp;&nbsp;&nbsp;&nbsp;<%}%>
</p>
</div>
</div>
</div>
<div class="row">
<% if @universe_scope %>
<div class="col s12 m4">
<div class="hoverable card">
<div class="card-content">
<div class="card-title">
Focus on another universe
</div>
<p>
You're currently focused on the
<%= link_to @universe_scope.name, @universe_scope, class: Universe.color + '-text' %>
universe and only seeing timelines in that
universe.
</p>
<br />
<p>
You can change what universe you're focused on by selecting a different one in the sidebar.
</p>
</div>
</div>
</div>
<% end %>
</div>
<% end %>

View File

@ -0,0 +1,5 @@
<h1>New Timeline</h1>
<%= render 'form', timeline: @timeline %>
<%= link_to 'Back', timelines_path %>

View File

@ -0,0 +1,4 @@
<p id="notice"><%= notice %></p>
<%= link_to 'Edit', edit_timeline_path(@timeline) %> |
<%= link_to 'Back', timelines_path %>

View File

@ -73,7 +73,7 @@ Rails.application.routes.draw do
get '/scratchpad', to: 'main#notes', as: :notes
# Legacy routes: left intact so /my/documents/X URLs continue to work for everyone's bookmarks
# Legacy route: left intact so /my/documents/X URLs continue to work for everyone's bookmarks
resources :documents
# Billing
@ -184,6 +184,7 @@ end
get '/tagged/:slug', action: :index, on: :collection, as: :page_tag
end
end
resources :timelines
# Content attributes
put '/content/sort', to: 'content#api_sort'
@ -195,9 +196,6 @@ end
# Attributes
get ':content_type/attributes', to: 'content#attributes', as: :attribute_customization
# Coming Soon TM
get '/plots', to: 'main#comingsoon'
end
get 'search/', to: 'search#results'

View File

@ -0,0 +1,48 @@
require 'test_helper'
class TimelinesControllerTest < ActionDispatch::IntegrationTest
setup do
@timeline = timelines(:one)
end
test "should get index" do
get timelines_url
assert_response :success
end
test "should get new" do
get new_timeline_url
assert_response :success
end
test "should create timeline" do
assert_difference('Timeline.count') do
post timelines_url, params: { timeline: { } }
end
assert_redirected_to timeline_url(Timeline.last)
end
test "should show timeline" do
get timeline_url(@timeline)
assert_response :success
end
test "should get edit" do
get edit_timeline_url(@timeline)
assert_response :success
end
test "should update timeline" do
patch timeline_url(@timeline), params: { timeline: { } }
assert_redirected_to timeline_url(@timeline)
end
test "should destroy timeline" do
assert_difference('Timeline.count', -1) do
delete timeline_url(@timeline)
end
assert_redirected_to timelines_url
end
end

View File

@ -0,0 +1,41 @@
require "application_system_test_case"
class TimelinesTest < ApplicationSystemTestCase
setup do
@timeline = timelines(:one)
end
test "visiting the index" do
visit timelines_url
assert_selector "h1", text: "Timelines"
end
test "creating a Timeline" do
visit timelines_url
click_on "New Timeline"
click_on "Create Timeline"
assert_text "Timeline was successfully created"
click_on "Back"
end
test "updating a Timeline" do
visit timelines_url
click_on "Edit", match: :first
click_on "Update Timeline"
assert_text "Timeline was successfully updated"
click_on "Back"
end
test "destroying a Timeline" do
visit timelines_url
page.accept_confirm do
click_on "Destroy", match: :first
end
assert_text "Timeline was successfully destroyed"
end
end