• Stars
    star
    171
  • Rank 215,407 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 13 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

store rails views on database instead of filesystem

Panoramic Build Status

An ActionView::Resolver implementation to store rails views (layouts, templates and partials) on database. Simply put: what you can do with views on filesystem, can be done on database.

NOTE: at the moment, only ActiveRecord is supported, I've planned to add more ORMs (see Todo). If you can't wait, adding other ORMs should be very trivial.

Installation

Add the following line to Gemfile:

gem "panoramic"

Usage

Mandatory fields

Your model should have the following fields:

  • body (text): the source of template
  • path (string): where to find template (ex: layouts/application, you_controller/action, etc...)
  • locale (string): it depends from available locales in your app
  • handler (string): as locale field, it depends from avaiable handlers (erb, haml, etc...)
  • partial (boolean): determines if it's a partial or not (false by default)
  • format (string): A valid mimetype from Mime::SET.symbols

they're what the rails' Resolver API needs to lookup templates.

Model

A simple macro in model will activate your new Resolver. You can use a dedicated model to manage all the views in your app, or just for specific needs (ex: you want a custom template for some static pages, the other views will be fetched from filesystem).

class TemplateStorage < ActiveRecord::Base
  store_templates
end

Controller

To add Panoramic::Resolver in controller, depending on your needs, you may choose:

  • prepend_view_path: search for templates first in your resolver, then on filesystem
  • append_view_path: search for templates first on filesystem, then in your resolver

NOTE: the above methods are both class and instance methods.

class SomeController < ApplicationController
  prepend_view_path TemplateStorage.resolver

  def index
    # as you may already know, rails will serve 'some/index' template by default, but it doesn't care where it is stored.
  end

  def show
    # explicit render
    render :template => 'custom_template'
  end

  def custom_template
    # use another model to fetch templates
    prepend_view_path AnotherModel.resolver
  end
end

And let's say you want to use database template resolving in all your controllers, but want to use panoramic only for certain paths (prefixed with X) you can use

class ApplicationController < ActionController::Base
  prepend_view_path TemplateStorage.resolver(:only => 'use_this_prefix_only')
end

This helps reducing the number of database requests, if Rails for example tries to look for layouts per controller.

ActionMailer

class MyEmail < ActionMailer::Base
  prepend_view_path TemplateStorage.resolver

Documentation

Need more help? Check out spec/dummy/, you'll find a dummy rails app I used to make tests ;-)

Testing

Enter Panoramic gem path, run bundle install to install development and test dependencies, then rake spec.

Todo

Long term

  • add generators

Contributing

Fork, make your changes, then send a pull request.

Credits

The main idea was heavily inspired from José Valim's awesome book Crafting Rails Applications. It helped me to better understand some Rails internals.

More Repositories

1

simple_form_fancy_uploads

simple_form custom inputs to get image/link previews with file uploads.
Ruby
164
star
2

nova

An attempt to port/rebuild Spree, an open source e-commerce solution, with Elixir and Phoenix.
Elixir
128
star
3

disco

Simple, opinionated yet flexible library to build CQRS/ES driven systems 🕺
Elixir
34
star
4

picciotto

minimalistic website framework based on Padrino
JavaScript
27
star
5

plug_ets_cache

A simple caching system based on Plug and ETS.
Elixir
26
star
6

flux-tweets

Realtime twitter stream built with NodeJS, React and Flux
JavaScript
17
star
7

12dos-bookmarks

A basic bookmark app
Ruby
17
star
8

rubyday

rubyday.it website
Ruby
9
star
9

mini_cqrs_es

Simple, minimal, opinionated building blocks to implement CQRS/ES in Rust
Rust
9
star
10

operation-go

JavaScript
8
star
11

dotfiles

~/.dotfiles
Lua
7
star
12

nodino

A simple URL shortener built with ExpressJS, React JS and Redis
JavaScript
6
star
13

avarus

a minimalistic URL shortener built with code parsimony in mind
Ruby
4
star
14

likes_tracker

track likes between rails models using Redis backend
Ruby
4
star
15

r12-guessthefriend

"Guess The Friend" game with your Facebook friends
Ruby
4
star
16

vim_starter_kit

a vim configuration with useful bundles
Vim Script
3
star
17

goshort

a simple URL shortener built with Go
CSS
3
star
18

ttfm_bot

A bot to bring some fun and utils on turntable.fm rooms
Go
3
star
19

flat

a semi static cms engine that runs as elixir phoenix web application
Elixir
3
star
20

parabellum

An attempt to build a #Travian 3.x clone written in #Rust.
Rust
3
star
21

gallerizer-jquery-plugin

A jQuery plugin that generates a simple crossfade slideshow
JavaScript
2
star
22

rbcue2txt

Parses cue files from Rekordbox's recordings to produce readable, markdown-friendly playlists.
Rust
2
star
23

robo_radio_rs

A Rust implementation of https://radio.pavonz.com
Rust
2
star
24

cash_cli

Basic CLI app to track personal financing written in #Rust
Rust
1
star
25

txtatus-cli

A very basic http://txtatus.com client to push statuses from command line
Go
1
star
26

stupeflix-client

A Ruby client for http://stupeflix.com API
Ruby
1
star
27

easy_config

Store Ruby or Rails app settings in the easiest way
Ruby
1
star
28

simple_counter

Simple counter experiment made with Rust, Axum, SeaORM, Askama templates and HTMx
Rust
1
star
29

scrobblify

Self-hosted music scrobble database to create personal listening statistics and charts from your Spotify (premium) account.
Rust
1
star