merged/app/models/merged/view_base.rb

99 lines
2.5 KiB
Ruby
Raw Normal View History

2022-12-05 17:14:56 +01:00
module Merged
2022-12-11 13:31:04 +01:00
2022-12-15 11:59:11 +01:00
# base class for viewable elements: ie page, section and casrd
2022-12-15 12:09:59 +01:00
# they share the template idea, options , change tracking,
# and the fact that they persist in ActiveYaml
2022-12-15 11:59:11 +01:00
2022-12-25 22:42:01 +01:00
class ViewBase < ActiveBase
2023-01-29 17:34:51 +01:00
set_root_path Rails.root.join(Merged.data_dir)
2022-12-15 11:59:11 +01:00
include ActiveHash::Associations
belongs_to :image , class_name: "Merged::Image"
2022-12-27 16:04:53 +01:00
fields :options
2022-12-15 12:09:59 +01:00
2022-12-26 19:58:34 +01:00
def last_update_for(elements)
last = Time.now
last_section = nil
elements.each do |section|
if( section.updated_at < last )
last = section.updated_at
last_section = section
end
end
last_section
end
2022-12-05 17:14:56 +01:00
def has_option?(option)
2022-12-07 20:47:06 +01:00
options.has_key?(option) and !options[option].blank?
2022-12-05 17:14:56 +01:00
end
def option_definitions
template_style.options_definitions
2022-12-05 17:14:56 +01:00
end
def option(name)
options[name]
end
def options
2022-12-11 13:31:04 +01:00
return attributes[:options] unless attributes[:options].blank?
attributes[:options] = {}
2022-12-05 17:14:56 +01:00
end
def set_option( option , value)
2023-01-08 22:48:20 +01:00
if( option.ends_with?"_date" )
2022-12-28 11:18:42 +01:00
puts "date is #{value}"
2023-01-08 22:48:20 +01:00
year = value[:year] || Time.new.year
value = Time.new( year.to_i , value[:month] , value[:day]).to_date
2022-12-28 11:18:42 +01:00
end
2022-12-05 17:14:56 +01:00
options[option] = value
end
2022-12-12 17:17:48 +01:00
def add_default_options( definitions = nil )
definitions = option_definitions if definitions.nil?
definitions.each do |option|
2022-12-11 15:27:08 +01:00
next unless option.default
set_option( option.name , option.default)
end
end
2023-01-20 14:40:41 +01:00
def update(hash)
2023-01-20 15:49:11 +01:00
return unless hash
2023-01-20 14:40:41 +01:00
hash.each do |key , value|
2023-01-20 18:54:20 +01:00
value = value.to_i if key.to_s.include?("_id")
key = key.to_sym
raise "unsuported field #{key} for #{template}:#{allowed_fields}" unless allowed_fields.include?(key)
2023-01-20 14:40:41 +01:00
if(! attributes[key].nil? ) # first setting ok, types not (yet?) specified
if( @attributes[key].class != value.class )
2023-01-20 18:54:20 +01:00
raise "Type mismatch #{key} #{@attributes[key].class}!=#{value.class}"
2023-01-20 14:40:41 +01:00
end
2022-12-11 13:31:04 +01:00
end
2023-01-20 14:40:41 +01:00
attributes[key] = value
2022-12-11 13:31:04 +01:00
end
end
2023-01-20 15:49:11 +01:00
def update_options( options )
return unless options
option_definitions.each do |option|
set_option(option.name, options[option.name])
end
end
#other may be nil
def swap_index_with(other)
return unless other
old_index = self.index
self.index = other.index
other.index = old_index
end
2022-12-11 13:31:04 +01:00
def allowed_fields
template_style.fields.collect{|f| f.to_sym}
end
def image_old
2022-12-17 12:56:42 +01:00
Image.find_by_name(self.image_name)
end
2022-12-05 17:14:56 +01:00
end
end