Permalink
Browse files

Simple one variable sort of working kinda maybe

  • Loading branch information...
1 parent 96a64ff commit 14cd0c3badf1b14701c9bd3fa94dbd859bdbc4a8 @willcodeforfoo committed Feb 14, 2012
Showing with 50 additions and 14 deletions.
  1. +23 −4 app.rb
  2. +2 −6 public/admin/stylesheets/styles.css
  3. +16 −1 spec/app_spec.rb
  4. +1 −1 spec/spec_helper.rb
  5. +7 −1 views/admin/edit.erb
  6. +1 −1 views/admin/new.erb
View
27 app.rb
@@ -126,13 +126,13 @@ def read_template_file(template_path, context)
erb :'admin/index'
end
-def render_template(template)
+def render_template(template, extra_assigns = {})
parsed_template = Liquid::Template.parse(template['body'])
assigns = {
'db' => CollectionDrop.new,
'lorem' => LoremDrop.new
- }
+ }.merge(extra_assigns)
content_type 'text/css' if template['path'].include?('.css')
content_type 'application/js' if template['path'].include?('.js')
@@ -143,10 +143,29 @@ def render_template(template)
get '*' do
route = params[:splat].join('/')
- template = $db['pages'].find(:path => route).first
+ # for routing named variables like /team/:team
+ # TODO Optimize this...
+ path_regexes = $db['pages'].find({}, :fields => "path", :sort => ['position', :asc]).map do |page|
+ [Regexp.new(page['path'].gsub(/(:\w+)/, '([A-z\-]+)') + "$"), page['_id']]
+ end
+
+ template = nil
+ path_regexes.each do |regexp, page_id|
+ if (match = route.match(regexp))
+ template = $db['pages'].find_one('_id' => page_id)
+
+ route_matches = route.match(regexp)
+
+ record = $db['team'].find_one('path' => route_matches[1])
+
+ return render_template(template, {'item' => RecordDrop.new(record)})
+ else
+ template = nil
+ end
+ end
if template.nil?
- custom_not_found_template = $db['pages'].find(:path => "404").first
+ custom_not_found_template = $db['pages'].find(:path => "/404").first
if custom_not_found_template
halt 404, render_template(custom_not_found_template)
else
@@ -28,12 +28,8 @@ a.delete:hover {
}
#editor {
- margin: 0;
- position: absolute;
- top: 150;
- bottom: 0;
- left: 0;
- right: 0;
+ width: 90%;
+ height: 600px;
}
ul.sortable li:hover {
View
@@ -21,14 +21,29 @@
last_response.body.should == "Team"
end
+ it "should GET a page with a variable in the path" do
+ $db['pages'].insert('path' => '/team/:team', 'title' => 'Team', 'body' => "Team Member {{item.name}} {{item.bio}}")
+
+ $db['team'].insert('name' => 'Paul Clark', 'path' => 'paul-clark', 'bio' => "was president of SFC")
+ $db['team'].insert('name' => 'Kevin Marsh', 'path' => 'kevin-marsh', 'bio' => "started SongMeanings")
+
+ get '/team/kevin-marsh'
+ last_response.should be_ok
+ last_response.body.should == "Team Member Kevin Marsh started SongMeanings"
+
+ get '/team/paul-clark'
+ last_response.should be_ok
+ last_response.body.should == "Team Member Paul Clark was president of SFC"
+ end
+
describe "missing pages" do
it "should return a 404 for a page that doesn't exist" do
get '/no-page-by-this-name'
last_response.status.should == 404
end
it "should return a custom 404 page if one exists" do
- $db['pages'].insert('path' => '404', 'title' => 'Team', 'body' => "Y U NO EXIST?!")
+ $db['pages'].insert('path' => '/404', 'title' => 'Team', 'body' => "Y U NO EXIST?!")
get '/no-page-by-this-name'
last_response.status.should == 404
View
@@ -20,6 +20,6 @@ def app
RSpec.configure do |config|
config.after(:each) do
- %w[items snippets pages projects].each {|collection| $db[collection].remove }
+ %w[items snippets pages projects team].each {|collection| $db[collection].remove }
end
end
View
@@ -1,5 +1,11 @@
<h2>Editing <%= display_for_record(@page) %> <span></span></h2>
+<form name="form">
+<% @page.keys.reject{|k| %w[_id body position versions].include? k }.each do |key| %>
+ <p><label><%= key %></label><br /><input name="<%= key %>" size="40" value="<%= @page[key] %>" /></p>
+<% end %>
+</form>
+
<pre id="editor"><%= h @page['body'] %></pre>
<script src="/admin/javascripts/ace/ace.js" type="text/javascript" charset="utf-8"></script>
@@ -14,7 +20,7 @@
$.ajax({
url: "/admin/<%= params[:collection] %>/<%= @page['_id'] %>",
type: "PUT",
- data: {"body": value },
+ data: $.extend($("form").serializeObject(), { "body": value }),
success: function(data) {
$("h2 span").text("(Saved)").delay(1500).fadeOut(250)
// TODO: Some kind of indication the page saved.
View
@@ -17,7 +17,7 @@
$.ajax({
url: "/admin/<%= params[:collection] %>",
type: "POST",
- data: $.extend($("form").serializeObject(), { "data": value }),
+ data: $.extend($("form").serializeObject(), { "body": value }),
success: function(data) {
$("h2 span").text("(Saved)").delay(1500).fadeOut(250)
// TODO: Some kind of indication the page saved.

0 comments on commit 14cd0c3

Please sign in to comment.