Skip to content

Commit

Permalink
- Added index page, preview
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiren Patel committed Apr 30, 2012
1 parent 2fb7b3d commit ef9d004
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 101 deletions.
2 changes: 1 addition & 1 deletion README → README.md
Expand Up @@ -6,4 +6,4 @@ Feather cms is a lightweght do it your self cms.

Gem : gem 'feather_cms'

rails g feather_cms about_us contect
rails g feather_cms about_us contact
1 change: 1 addition & 0 deletions lib/feather_cms.rb
Expand Up @@ -13,6 +13,7 @@ def self.included(base)
if Config.template_store_type == :file
base.define_feather_cms_callbacks
end
base.template_content_field :content
base.send(:include, FeatherCms::Model::InstanceMethods)
end
end
24 changes: 13 additions & 11 deletions lib/feather_cms/config.rb
Expand Up @@ -14,13 +14,18 @@ def init(&block)
yield self if block_given?

template_store_type = (template_store_type || :file).to_sym
FeatherCms::TemplateCache.init({:max_limit => cache_max_limit,
:permanent_keys => cache_permanent_keys})
FeatherCms::TemplateCache.init
Dir.mkdir(template_store_path) unless Dir.exists?(template_store_path)

if defined?(ActionView::Helpers)
ActionView::Helpers.send(:include, FeatherCms::ViewHelper)
end

if defined?(Rails)
@@config[:layouts] = Dir.entries(Rails.root.to_s + '/app/views/layouts').reject do |i|
i.start_with?('.', '_', 'feather_layout')
end.collect{|l| l.split('.').first}
end
end

def template_store_path=(value)
Expand All @@ -31,21 +36,18 @@ def template_store_path
@@config[:template_store_path]
end

def use_version=(value)
@@config[:use_version] = value
end

def use_version
@@config[:use_version]
end

def template_extenstion=(value)
@@config[:template_extenstion] = value
end

def template_extenstion
@@config[:template_extenstion]
@@config[:template_extenstion]
end

def layouts
@@config[:layouts]
end

end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/feather_cms/model.rb
Expand Up @@ -51,7 +51,7 @@ def _template_path(field)

def _cms_content(field)
return nil if self.id.nil?
TemplateCache.get_or_cache_file(template_name(field))
TemplateCache.cache_file(template_name(field))
end

end
Expand Down
66 changes: 12 additions & 54 deletions lib/feather_cms/template_cache.rb
Expand Up @@ -3,91 +3,49 @@ class TemplateCache
class << self

def init(options = {})
@@_cache_map ||= {}
@@_cache ||= {}
@@options = options
@@options[:max_limit] ||= 20
@@_callbacks = {:before => [], :after => []}

unless @@options[:permanent_keys].is_a?(Array)
@@options[:permanent_keys] = [@@options[:permanent_keys]]
end
@@options[:permanent_keys] = @@options[:permanent_keys].compact

self
end

def [](key)
@@_cache_map[key.to_s]
@@_cache[key.to_s]
end

def []=(k,v)
clear_all if @@_cache_map.length == @@options[:max_limit]

@@_cache_map[k.to_s] = _call_callbacks(:before, k, v)
_call_callbacks(:after, k, v)
@@_cache[k.to_s] = v
end

def cache_file(name, file = nil)
return self[name] unless file
self[name] = File.read(file) if File.exist?(file)
end
def cache_file(name)
return self[name] if exist?(name)

def get_or_cache_file(file)
self[file] || cache_file(file, _file_path(file))
file = _file_path(name)
self[file] = File.read(file) if File.exist?(file)
end

def write_to_file_and_cache(content, file)
return if get_or_cache_file(file) == content
return if cache_file(file) == content
self[file] = content
File.open(_file_path(file), 'wb') {|f| f.write(content) }
end

def exist?(name)
@@_cache_map.key?(name.to_s)
@@_cache.key?(name.to_s)
end

def clear_all
@@_cache_map.delete_if{|k,v| !@@options[:permanent_keys].include?(k)}
end

def clear(name)
@@_cache_map.delete(name.to_s)
def clear(name = nil)
name ? @@_cache.delete(name.to_s) : @@_cache.clear
end

def delete_file(name)
file = _file_path(name)
File.delete(file) if File.exist?(file)
@@_cache_map.delete(name)
end

def before_add(options = {}, &block)
_register_callback(:before, options, &block)
end

def after_add(options = {}, &block)
_register_callback(:after, options, &block)
end

def view_callbacks
@@_callbacks.inspect
@@_cache.delete(name)
end

private
def _file_path(name)
File.join([Config.template_store_path, name])
end

def _register_callback(type, options, &block)
@@_callbacks[type] << [@@_callbacks[type].length + 1, options, block]
end

def _call_callbacks(type, key, value)
@@_callbacks[type].each do |callback|
value = callback.last.call(key, value)
end
value
end

end

end
Expand Down
22 changes: 10 additions & 12 deletions lib/generators/feather_cms/feather_cms_generator.rb
Expand Up @@ -4,8 +4,6 @@ class FeatherCmsGenerator < Rails::Generators::NamedBase
argument :name, :type => :string, :default => "feather_cms", :required => false
argument :storage, :type => :string , :default => 'file'

#PAGES = ['about_us']

def create_cms_files
@pages = attributes.collect(&:name)

Expand All @@ -28,24 +26,24 @@ def create_cms_files

template 'migration.rb', "db/migrate/#{migration_number}_create_feather_pages.rb"

route_str = "scope '/feathers' do \n"
@pages.each do |action|
route_str << %{ match '#{action}_page/(:status)' => 'feathers##{action}', :as => :feather_#{action} \n}
end
route_str << " end"
route route_str

route %{scope '/feathers' do
match 'page/:type/(:status)' => 'feathers#page', :as => :feather_page
get 'pages' => 'feathers#index', :as => :feather_pages
get 'preivew/:type/(:status)' => 'feathers#preivew', :as => 'feather_page_preview'
end
get 'page/:type' => 'feathers#published', :as => 'feather_published_page'}
end

def copy_view_files
@pages = attributes.collect(&:name)
base_path = File.join("app/views/feathers")
#empty_directory base_path
template 'layout.html.erb', 'app/views/layouts/feather_layout.html.erb'
template 'index.html.erb', File.join(base_path, 'index.html.erb')

@pages.each do |action|
@action = action
@path = File.join(base_path, "#{action}.html.erb")
@pages.each do |type|
@type = type
@path = File.join(base_path, "#{type}.html.erb")
template 'form.html.erb', @path
end
end
Expand Down
29 changes: 21 additions & 8 deletions lib/generators/feather_cms/templates/controller.rb
@@ -1,21 +1,34 @@
class FeathersController < ApplicationController
http_basic_authenticate_with :name => "feather", :password => "password"
layout 'feather_layout'
before_filter :find_page
http_basic_authenticate_with name: 'feather', password: 'password', except: :published
before_filter :find_page, only: [:page, :preivew]

<% @pages.each do |page| %>
def <%= page %>
layout 'feather_layout', except: [:preivew, :published]

def index
@pages = FeatherPage.all
end

def page
if request.put? or request.post?
@feather_page.attributes = params[:feather_page]
@feather_page.name = "<%= page %>"
@feather_page.name = params[:type]
@feather_page.save
end
render action: @feather_page.name
end

def preivew
render inline: @feather_page.content, type: 'html', layout: @feather_page.layout
end

def published
@feather_page = FeatherPage.where(name: params[:type], status: 'published').first
render inline: @feather_page.content, type: 'html', layout: @feather_page.layout
end
<% end %>

def find_page
status = params[:feather_page] ? params[:feather_page][:status] : (params[:status] || 'draft')
@feather_page = FeatherPage.where(:name => params[:action], :status => status)
@feather_page = FeatherPage.where(name: params[:type], status: status)
@feather_page = @feather_page.first || @feather_page.new
end
end
15 changes: 11 additions & 4 deletions lib/generators/feather_cms/templates/form.html.erb
Expand Up @@ -3,23 +3,30 @@
<div class="subnav">
<ul class="nav nav-pills">
<li class='<%%= status == 'published' ? 'active ' : '' %>'>
<%%= link_to "Published Page", feather_<%=@action%>_path(:published) %>
<%%= link_to 'Published Page', feather_page_path('<%= @type %>',:published) %>
</li>
<li class='<%%= status == 'draft' ? 'active ' : '' %>'>
<%%= link_to "Draft Page", feather_<%=@action%>_path(:draft) %>
<%%= link_to 'Draft Page', feather_page_path('<%= @type %>', :draft) %>
</li>
<li>
<%%= link_to 'Preview', feather_page_preview_path('<%= @type %>', status), target: '_blank' %>
</li>
</ul>
</div>
<hr>
</header>
<div class="row">
<div class="span12">
<%%= form_for(@feather_page, :url => feather_<%= @action %>_path) do |f| %>
<%%= form_for(@feather_page, :url => feather_page_path('<%= @type %>')) do |f| %>
<div class="control-group">
<label class="control-label" for="select01">Status</label>
<label class="control-label" for="status">Status</label>
<div class="controls">
<%%= f.select :status, [:draft, :published] %>
</div>
<label class="control-label" for="layout">Layout</label>
<div class="controls">
<%%= f.select :layout, FeatherCms::Config.layouts %>
</div>
</div>
<div class="control-group">
<label class="control-label" for="textarea">Content</label>
Expand Down
11 changes: 11 additions & 0 deletions lib/generators/feather_cms/templates/index.html.erb
@@ -0,0 +1,11 @@
<div class="row">
<ol>
<%% @pages.each do |page| %>
<li>
<strong>
<%%= link_to "#{page.name.humanize}(#{page.status})", feather_page_path(page.name, page.status) %>
</strong>
</li>
<%% end %>
</ol>
</div>
1 change: 0 additions & 1 deletion lib/generators/feather_cms/templates/initializer.rb
@@ -1,6 +1,5 @@
FeatherCms::Config.init do |c|
c.template_store_path = 'public/system/templates'
c.template_store_type = :<%= @storage %>
#c.cache_max_limit = 50 #default : 20
#c.template_extenstion = 'html' #default : html
end
12 changes: 6 additions & 6 deletions lib/generators/feather_cms/templates/layout.html.erb
Expand Up @@ -30,14 +30,14 @@
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">Feather CMS</a>
<%%= link_to 'Feather CMS', feather_pages_path, :class => 'brand' %>
<div class="nav-collapse">
<ul class="nav">
<%- @pages.each do |page| %>
<li class="<%%= params[:action] == '<%=page%>' ? 'active' : '' %>">
<%%= link_to '<%= page.humanize %>' , feather_<%=page%>_path %>
</li>
<% end -%>
<%- @pages.each do |page| -%>
<li class="<%%= params[:type] == '<%=page%>' ? 'active' : '' %>">
<%%= link_to '<%= page.humanize %>' , feather_page_path('<%= page %>') %>
</li>
<%- end -%>
</ul>
</div><!--/.nav-collapse -->
</div>
Expand Down
5 changes: 3 additions & 2 deletions lib/generators/feather_cms/templates/migration.rb
Expand Up @@ -3,9 +3,10 @@ def change
create_table :feather_pages do |t|
t.string :name
t.string :status, :default => 'draft'
<% if @storaage == :db %>
t.text :content
<% if @storaage == 'db' %>
t.text :content
<% end %>
t.string :layout, :default => 'application'
t.timestamps
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/generators/feather_cms/templates/model.rb
@@ -1,4 +1,3 @@
class FeatherPage < ActiveRecord::Base
include FeatherCms
template_content_field :content
end

0 comments on commit ef9d004

Please sign in to comment.