moving models to the oh so clean active_hash, puny tests work
This commit is contained in:
@ -1,30 +1,13 @@
|
||||
module Merged
|
||||
class Card
|
||||
include ActiveModel::API
|
||||
include ActiveModel::Conversion
|
||||
extend ActiveModel::Naming
|
||||
class Card < ActiveYaml::Base
|
||||
set_root_path Rails.root #ouside engines not necessary
|
||||
|
||||
include ActiveHash::Associations
|
||||
belongs_to :section , class_name: "Merged::Section"
|
||||
|
||||
include Optioned
|
||||
|
||||
cattr_reader :all
|
||||
@@all = {}
|
||||
|
||||
attr_reader :content , :index , :section
|
||||
|
||||
[ :id , :text , :header, :image ].each do |meth|
|
||||
define_method(meth) do
|
||||
@content[meth.to_s]
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(section , index , card_data)
|
||||
@section = section
|
||||
@index = index
|
||||
raise "No data #{card_data}" unless card_data.is_a?(Hash)
|
||||
@content = card_data
|
||||
raise "No id #{card_data}" unless id.is_a?(String)
|
||||
@@all[self.id] = self
|
||||
end
|
||||
fields :index , :section_id, :id , :text , :header, :image
|
||||
|
||||
def template_style
|
||||
@section.card_template
|
||||
@ -52,11 +35,8 @@ module Merged
|
||||
end
|
||||
|
||||
def save
|
||||
section.save
|
||||
end
|
||||
def save_soon
|
||||
super
|
||||
data = Option.all.collect {|obj| obj.attributes}
|
||||
data = Card.all.collect {|obj| obj.attributes}
|
||||
File.write( Option.full_path , data.to_yaml)
|
||||
end
|
||||
|
||||
|
@ -1,48 +1,18 @@
|
||||
module Merged
|
||||
class Page
|
||||
include ActiveModel::API
|
||||
include ActiveModel::Conversion
|
||||
extend ActiveModel::Naming
|
||||
class Page < ActiveYaml::Base
|
||||
set_root_path Rails.root #ouside engines not necessary
|
||||
include ActiveHash::Associations
|
||||
has_many :sections , class_name: "Merged::Section"
|
||||
|
||||
# could be config options
|
||||
def self.cms_root
|
||||
"cms"
|
||||
end
|
||||
|
||||
cattr_reader :all
|
||||
@@all = {}
|
||||
|
||||
def self.load_pages()
|
||||
files = Set.new Dir.new(Rails.root.join(Page.cms_root)).children
|
||||
files.each do |file|
|
||||
page = Page.new(file)
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :name , :content , :sections , :size , :updated_at
|
||||
fields :name , :content , :size , :updated_at
|
||||
|
||||
alias :id :name
|
||||
|
||||
def initialize( f_name )
|
||||
@name = f_name.split(".").first
|
||||
@content = YAML.load_file( filename )
|
||||
@sections = []
|
||||
@content.each_with_index do |section_data, index|
|
||||
section = Section.new(self , index, section_data)
|
||||
@sections << section
|
||||
end
|
||||
@@all[@name] = self
|
||||
update_size
|
||||
end
|
||||
|
||||
def filename
|
||||
Rails.root.join(Page.cms_root , @name + ".yaml")
|
||||
end
|
||||
def update_size
|
||||
@size = File.size(filename)
|
||||
@updated_at = File.ctime(filename)
|
||||
end
|
||||
|
||||
def self.check_name(name)
|
||||
return "only alphanumeric, not #{name}" if name.match(/\A[a-zA-Z0-9]*\z/).nil?
|
||||
nil
|
||||
@ -111,15 +81,9 @@ module Merged
|
||||
end
|
||||
|
||||
def save
|
||||
File.write( filename , @content.to_yaml)
|
||||
update_size
|
||||
end
|
||||
|
||||
def self.find(name)
|
||||
raise "nil given" if name.blank?
|
||||
page = @@all[name]
|
||||
raise "Page not found #{name}" unless page
|
||||
return page
|
||||
super
|
||||
data = Page.all.collect {|obj| obj.attributes}
|
||||
File.write( Page.full_path , data.to_yaml)
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -1,50 +1,25 @@
|
||||
module Merged
|
||||
class Section
|
||||
include ActiveModel::API
|
||||
include ActiveModel::Conversion
|
||||
extend ActiveModel::Naming
|
||||
class Section < ActiveYaml::Base
|
||||
set_root_path Rails.root #ouside engines not necessary
|
||||
|
||||
include ActiveHash::Associations
|
||||
belongs_to :page , class_name: "Merged::Page"
|
||||
has_many :cards , class_name: "Merged::Card"
|
||||
|
||||
include Optioned
|
||||
|
||||
cattr_reader :all
|
||||
@@all = {}
|
||||
|
||||
attr_reader :name , :content , :page , :index , :cards
|
||||
|
||||
def initialize(page , index , section_data)
|
||||
@page = page
|
||||
raise "No number #{index}" unless index.is_a?(Integer)
|
||||
raise "No hash #{section_data}" unless section_data.is_a?(Hash)
|
||||
@index = index
|
||||
@content = section_data
|
||||
@@all[self.id] = self
|
||||
@cards = []
|
||||
element = @content["cards"]
|
||||
return if element.nil?
|
||||
element.each_with_index do|card_content , index|
|
||||
@cards << Card.new(self , index , card_content)
|
||||
end
|
||||
end
|
||||
|
||||
[:template , :card_template , :id , :text , :header, :image].each do |meth|
|
||||
define_method(meth) do
|
||||
@content[meth.to_s]
|
||||
end
|
||||
end
|
||||
|
||||
fields :name , :page_id , :index , :cards
|
||||
fields :template , :card_template , :id , :text , :header, :image
|
||||
|
||||
def set_template(new_template)
|
||||
@content["template"] = new_template
|
||||
self.template = new_template
|
||||
new_style = template_style
|
||||
if(new_style.has_cards?)
|
||||
unless card_template
|
||||
@content["card_template"] = CardStyle.first.name
|
||||
@content["cards"] = []
|
||||
raise "Should not have cards" unless cards.empty?
|
||||
self.card_template = CardStyle.first.name
|
||||
end
|
||||
else
|
||||
@content.delete("cards")
|
||||
@content.delete("card_template")
|
||||
@cards.clear
|
||||
cards.delete_all
|
||||
end
|
||||
end
|
||||
|
||||
@ -138,7 +113,9 @@ module Merged
|
||||
end
|
||||
|
||||
def save
|
||||
page.save
|
||||
super
|
||||
data = Section.all.collect {|obj| obj.attributes}
|
||||
File.write( Section.full_path , data.to_yaml)
|
||||
end
|
||||
|
||||
def set_index(index)
|
||||
|
Reference in New Issue
Block a user