Skip to content

Instantly share code, notes, and snippets.

@patrick99e99
Created October 14, 2008 17:46
Show Gist options
  • Save patrick99e99/16754 to your computer and use it in GitHub Desktop.
Save patrick99e99/16754 to your computer and use it in GitHub Desktop.
t_for :webpage_title do -%>
<%= @webpage.title if @webpage %>
<%- end -%>
<%= display_html_content(@contents) unless @contents.nil? %>
class CreateContents < ActiveRecord::Migration
def self.up
create_table :contents do |t|
t.text :data
t.integer :layout_order
t.integer :element_id
t.integer :webpage_id
t.integer :parent_id
t.timestamps
end
end
def self.down
drop_table :contents
end
end
class CreateElements < ActiveRecord::Migration
def self.up
create_table :elements do |t|
t.string :markup
t.text :style
t.timestamps
end
end
def self.down
drop_table :elements
end
end
class Webpage < ActiveRecord::Base
belongs_to :dropdown
has_many :contents, :order => "parent_id, layout_order"
end
class Content < ActiveRecord::Base
belongs_to :element
belongs_to :webpage
end
class Element < ActiveRecord::Base
has_many :contents
end
task :db_initialize => :environment do
# -------------------
# 1
e = Element.new
e.markup = "p"
e.style = "color: red;"
e.save!
# 2
e = Element.new
e.markup = "h2"
e.style = "text-align: center;"
e.save!
# 3
e = Element.new
e.markup = "li"
e.style = "text-align: right; list-style-type: disc;"
e.save!
# 4
e = Element.new
e.markup = "div"
e.style = "float: left; border: 1px solid red; width: 48%;"
e.save!
# 5
e = Element.new
e.markup = "div"
e.style = "float: right; border: 1px solid blue;"
e.save!
# 6
e = Element.new
e.markup = "div"
e.style = "width: 50%; text-align: center; border: 1px solid teal;"
e.save!
# 7
e = Element.new
e.markup = "ul"
e.style = "border: 1px solid green;"
e.save!
#8
e = Element.new
e.markup = "div"
e.style = "background: #9d10ff; margin: 5px; padding: 15%; float: left; border: 1px solid #333;"
e.save!
# -------------------
#red div
c = Content.new
c.element_id = 4
c.webpage_id = 1
c.layout_order = 1
c.parent_id = nil
c.data = nil
c.save!
left_div = c.id
#red div
c = Content.new
c.element_id = 4
c.webpage_id = 1
c.layout_order = 2
c.parent_id = nil
c.data = nil
c.save!
left2_div = c.id
#p
c = Content.new
c.element_id = 1
c.webpage_id = 1
c.layout_order = 2
c.parent_id = left_div
c.data = "I should be a 2nd paragraph in the left div"
c.save
#p
c = Content.new
c.element_id = 1
c.webpage_id = 1
c.layout_order = 1
c.parent_id = left_div
c.data = "I am a paragraph, meant to be in the left div"
c.save
#purple box
c = Content.new
c.element_id = 8
c.webpage_id = 1
c.layout_order = 3
c.parent_id = left2_div
c.save!
purple = c.id
#ul
c = Content.new
c.element_id = 7
c.webpage_id = 1
c.layout_order = 1
c.parent_id = purple
c.save!
ul_id = c.id
num = 0
5.times do
num += 1
c = Content.new
c.element_id = 3
c.webpage_id = 1
c.layout_order = num
c.parent_id = ul_id
c.data = "inside purple item number#{num}"
c.save!
end
end
end
class WebpagesController < ApplicationController
# GET /webpages
# GET /webpages.xml
def index
@webpage = Webpage.find_by_id(params[:page])
@contents = @webpage.contents unless @webpage.nil?
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @webpages }
end
end
end
module WebpagesHelper
def display_html_content(contents)
$completed_elements = []
contents.map do |c|
unless $completed_elements.include?(c.id)
get_nested_elements(c, contents)
end
end
end
def get_nested_elements(c, contents)
nested_items = contents.find_all_by_parent_id(c.id)
content_tag c.element.markup, :style => c.element.style do
nested = c.data.nil? ? "" : c.data
unless nested_items.blank?
nested_items.map do |n|
nested += get_nested_elements(n, contents)
end
end
$completed_elements << c.id
nested
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment