Skip to content

Instantly share code, notes, and snippets.

@jamesmk
Last active August 29, 2015 14:08
Show Gist options
  • Save jamesmk/b42926f4977167514913 to your computer and use it in GitHub Desktop.
Save jamesmk/b42926f4977167514913 to your computer and use it in GitHub Desktop.
Content blocks schema
/ in ASP
.summary = @page.company_summary.content
.quote
= @page.quote.content
em = @page.quote_attribution.content
# in ASP
class HomePage < Fae::Page
include Fae::Concerns::Models::Base
@@slug = 'home'
# start the crazy...
# dynamically build associations and forms?
# def fields
# {
# company_summary: 'TextArea',
# quote_attribution: 'TextField'
# }
# end
has_one :company_summary, -> { where(attached_as: 'company_summary' ) },
as: :contentable,
class_name: 'Fae::TextArea',
dependent: :destroy
accepts_nested_attributes_for :company_summary, allow_destroy: true
has_one :quote, -> { where(attached_as: 'quote' ) },
as: :contentable,
class_name: 'Fae::TextArea',
dependent: :destroy
accepts_nested_attributes_for :quote, allow_destroy: true
has_one :quote_attribution, -> { where(attached_as: 'quote_attribution' ) },
as: :contentable,
class_name: 'Fae::TextField',
dependent: :destroy
accepts_nested_attributes_for :quote_attribution, allow_destroy: true
end
# in Fae
module Fae
class Page < ActiveRecord::Base
def self.instance
row = find_by_slug(@@slug)
if row.blank?
row = Page.create({title: @@slug.humanize, slug: @@slug})
#row.singleton_guard = 0
#row.save!
end
row
end
end
# in ASP
class PagesController < ApplicationController
def home
@page = HomePage.instance
end
end
module Fae
class StaticPagesController < ApplicationController
before_action :set_item, only: [:edit, :update, :destroy]
helper FormHelper
def index
#override in app
# @blocks ||= [HomePage, AboutPage]
@items = blocks.map { |b| b.instance } if blocks.present?
end
# def index
# blocks = [Careers, HomePage]
# super
# end
def edit
build_assocs
render template: @item.slug
end
def update
if @item.update(item_params)
redirect_to @index_path, notice: "Success. You’ve done good."
else
build_assocs
render action: 'edit', error: "Let’s slow down a bit. Check your form for errors."
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_item
page = Fae::Page.find_by_slug(params[:slug])
@item = page.subclass.constitize.instance
end
# Only allow a trusted parameter "white list" through.
def item_params
params.require(@klass_base.singularize).permit!
end
# if model has images or files, build them here for nesting
def build_assocs
@item.reflections.each do |assoc|
#build assoc
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment