Rendering & filtering

Perron's resources are just Ruby objects so it is straight-forward to render, filter and order resources.

Resource content

To render a resource's content, use @resource.content.

<%= @resource.content %>

Rendering resources

Render a list of resources with: Content::Post.all. Pass the array to render, just like with ActiveRecord models:

<%= render Content::Post.all %>

This expects a partial app/views/content/posts/_post.html.erb.

Or set a partial and pass the collection:

<%= render partial: "content/posts/snippet", collection: Content::Post.all %>

ActiveRecord-style queries

v0.18.0+

Perron supports familiar ActiveRecord-style query methods for cleaner, more readable code.

Where

Filter resources using hash syntax:

Content::Post.where(category: "ruby")
Content::Post.where(published: true)
Content::Post.where(section: [:content, :metadata])

Order

Sort resources by attributes:

Content::Post.order(:title)
Content::Post.order(:publication_date, :desc)
Content::Post.order(title: :desc)

Limit

Limit the number of results:

Content::Post.limit(5)
Content::Post.order(:publication_date, :desc).limit(3)

Offset

Offset the number of results:

Content::Post.offset(2)
Content::Post.offset(2).limit(5)

Scopes

Define reusable query scopes:

class Content::Post < Perron::Resource
  scope :getting_started, -> { where(section: :getting_started) }
  scope :recent, -> { order(:publication_date, :desc).limit(10) }
end

Content::Post.getting_started.order(:title).limit(5)

Chaining

Chain methods together for complex queries:

Content::Post
  .where(published: true)
  .order(:publication_date, :desc)
  .limit(5)

Enumerable methods

All standard Ruby enumerable methods are available: select, reject, map, find, group_by, sort_by, count, any?, all?, first, last and more.

Content::Post.all.select { it.published? }
Content::Post.all.group_by(&:category)
Content::Post.all.sort_by(&:publication_date).reverse