Skip to content

Using Custom Post Types

David Van Der Beek edited this page May 5, 2015 · 4 revisions

Storytime supports custom post types (post types other than Storytime::BlogPost) and takes the opinion that these are a concern of the host app. To add a custom post type, define a new model in your host app that inherits from Storytime::BlogPost or Storytime::Page.

# app/models/video_post.rb
class VideoPost < Storytime::BlogPost
end

You then need to register the post type in your Storytime initializer:

# config/initializers/storytime.rb
Storytime.configure do |config|
  config.post_types += ["VideoPost"]
end

Overriding Post Type Options

show_comments?

show_comments? determines whether comments will be shown on the post. By default, show_comments? is set to true for BlogPosts and false for Pages.

# app/models/video_post.rb
class VideoPost < Storytime::BlogPost
  def show_comments?
    false
  end
end

Custom Fields

You can also add fields to the post form for your custom type. First, since your custom post model inherits from Storytime::BlogPost or Storytime::Page, you'll need to create a migration to add the custom fields to the storytime_posts table in your database. Then, In the host app, add a partial for your fields: app/views/storytime/dashboard/posts/_your_post_type_fields.html.erb, where your_post_type is the underscored version of your custom post type class (the example class above would be _video_post_fields.html.erb). This partial will be included in the form and passed a form builder variable named f.

For example, if we had created a migration in the host app to add featured_media_caption and featured_media_ids fields to the storytime_posts table, we could do the following:

<%# app/views/storytime/dashboard/posts/_video_post_fields.html.erb %>
<%= f.input :featured_media_caption %>
<%= f.input :featured_media_ids %>

Whitelisting Custom Fields

Any custom field that you want to edit through the post form must also passed to Storytime for whitelisting through the storytime_post_params_additions method in your ApplicationController.

# app/controllers/application_controller.rb
def storytime_post_param_additions
  attrs = [:featured_media_caption, {:featured_media_ids => []}]
  attrs
end

Custom Show Views

To create a custom #show view for your custom type, we could add one to app/views/storytime/your-site-title/your_post_type/show.html.erb, where your_post_type is the underscored version of your custom post type class (the example class above would be video_post).