Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/floere/contexts

Simple sidebars (or Cart, or Components) for Rails
https://github.com/floere/contexts

Last synced: 2 months ago
JSON representation

Simple sidebars (or Cart, or Components) for Rails

Awesome Lists containing this project

README

        

h1. Contexts

*A Rails 2 Plugin. This plugin is a simple alternative for components*.

Feedback please to @florian.hanke+contexts at gmail.com@, thanks! :)

h2. Simple components alternative

This plugin is a simple alternative for components.

It is not a full replacement, however. It aims lower, namely:
* Sub-per-action component definitions.
* Built-in caching.

h2. It is good, when:

# You wish to define certain view parts that are used for many controller actions.
# You wish to cache these view parts.
# You wish to not have specific controllers to load the content for these view parts.

h3. Example use cases

* Checkout Cart displayed in site sidebar.
* Explorative Elements (e.g. Top Ten Books) in Sidebars.
* Navigational Elements on almost all pages.

And so on…

h2. Usage

In the view, e.g. application.haml call

render_context context_category_name

h3. Examples

h4. Context chosen defined in the controller.

render_context :left_sidebar

In this case, the specific context is determined by the controller, just
define the context for this controller as follows:

context context_category_name,

default_context_name,
[action_name, other_action_name] => action_specific_context_name,
some_other_action_name => yet_another_action_specific_context_name

*OR* by using a block

context context_category_name do

# determine a context type as you wish (e.g. randomly),
# then return the context name
end

Use @top_ten_books@ as context for the context category @left_sidebar@ in _all_ actions:

context :left_sidebar, :top_ten_books

Use top_ten_books as context for the context category @left_sidebar@ in all
actions _except_ @buy@, @browse@ and @login@. Use @other_books_you_might_like@
for @buy@ and @browse@, and @welcome@ for the @login@ action:

context :left_sidebar, :top_ten_books,

[:buy, :browse] => :other_books_you_might_like,
:login => :welcome

*OR* if the specific context type should _not_ be determined by the controller.

render_context context_category_name, context_type_name

h4. Explicit context (Context not determined in the controller).

The following just renders the context for the top ten books in the left sidebar
without asking the controller to determine which context type should be used for
the left sidebar.

render_context :left_sidebar, :top_ten_books

h4. Loading variables for your contexts.

Loading variables for your contexts is done in the ApplicationController
(or if it should not be available everywhere in the Controller needed)

In your ApplicationController call the following to load instance variables for the context in category and type.

load_context(category, type, options = {}, &loading_instance_variables_block)

Currently supported options are @cache@, e.g.:

:cache => 7.minutes

This loads the top ten books into the variable @@books@:

load_context :left_sidebar, :top_ten_books, :cache => 5.minutes do

@books = Books.top(10)
end

which can then be used in the partial @contexts/left_sidebar/_top_ten_books.html.haml@:

%h1 Top Ten Books

- for book in @books do
%h2= book.title
%p= book.description
= link_to_add_to_cart(book)

The context view files should be in @views/contexts//.html.haml@ (or @.text.erb@ or what have you, depending on the request format)

The file in the example above would be in:
@app/views/contexts/left_sidebar/top_ten_books.html.haml@