Class: Breeze::ViewBase

Inherits:
ActiveBase show all
Defined in:
app/models/breeze/view_base.rb

Overview

base class for viewable elements: ie page, section and casrd they share the template idea, options , change tracking, and the fact that they persist in ActiveYaml

Direct Known Subclasses

Card, Page, Section

Instance Attribute Summary

Attributes inherited from ActiveYaml

#data

Instance Method Summary collapse

Methods inherited from ActiveBase

#add_save, #add_save!, #delete_save!, #edit_save, #edit_save!

Methods inherited from ActiveYaml

all, append, define_access, define_association, delete, fields, find, find_all, find_by, first, full_path, #id, #id=, #initialize, load_file, #persisted?, primary_key, reload, save_all, set_root_path, the_meta_class

Constructor Details

This class inherits a constructor from Breeze::ActiveYaml

Instance Method Details

#add_default_options(definitions = nil) ⇒ Object



64
65
66
67
68
69
70
# File 'app/models/breeze/view_base.rb', line 64

def add_default_options( definitions = nil )
  definitions = option_definitions if definitions.nil?
  definitions.each do |option|
    next unless option.default
    set_option( option.name , option.default)
  end
end

#allowed_fieldsObject



101
102
103
# File 'app/models/breeze/view_base.rb', line 101

def allowed_fields
  template_style.fields.collect{|f| f.to_sym}
end

#get_translation(lang) ⇒ Object



109
110
111
# File 'app/models/breeze/view_base.rb', line 109

def get_translation(lang)
  Translation.new( self , lang)
end

#has_option?(option) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/breeze/view_base.rb', line 37

def has_option?(option)
  safe_options.has_key?(option) and !safe_options[option].blank?
end

#header_text(lang) ⇒ Object



11
12
13
14
# File 'app/models/breeze/view_base.rb', line 11

def header_text(lang)
  return self.header if lang.blank?
  get_translation(lang).header
end

#image_oldObject



105
106
107
# File 'app/models/breeze/view_base.rb', line 105

def image_old
  Image.find_by_name(self.image_name)
end

#last_update_for(elements) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/breeze/view_base.rb', line 25

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

#option(name) ⇒ Object



45
46
47
48
# File 'app/models/breeze/view_base.rb', line 45

def option(name)
  opts = safe_options
  opts[name]
end

#option_definitionsObject



41
42
43
# File 'app/models/breeze/view_base.rb', line 41

def option_definitions
  template_style.options_definitions
end

#safe_optionsObject



50
51
52
53
# File 'app/models/breeze/view_base.rb', line 50

def safe_options
  return data[:options] unless data[:options].blank?
  data[:options] = {}
end

#set_option(option, value) ⇒ Object



55
56
57
58
59
60
61
62
# File 'app/models/breeze/view_base.rb', line 55

def set_option( option , value)
  if( option.ends_with?"_date" )
    puts "date is #{value}"
    year = value[:year] || Time.new.year
    value = Time.new( year.to_i , value[:month] , value[:day]).to_date
  end
  safe_options[option] = value
end

#swap_index_with(other) ⇒ Object

other may be nil



94
95
96
97
98
99
# File 'app/models/breeze/view_base.rb', line 94

def swap_index_with(other)
  return unless other
  old_index = self.index
  self.index = other.index
  other.index = old_index
end

#text_text(lang) ⇒ Object



16
17
18
19
# File 'app/models/breeze/view_base.rb', line 16

def text_text(lang)
  return self.text if lang.blank?
  get_translation(lang).text
end

#type_idObject



21
22
23
# File 'app/models/breeze/view_base.rb', line 21

def type_id
  "#{object_type}_#{self.id}"
end

#update(hash) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/breeze/view_base.rb', line 72

def update(hash)
  return unless hash
  hash.each do |key , value|
    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)
    if(! data[key].nil? ) # first setting ok, types not (yet?) specified
      if( data[key].class != value.class )
        raise "Type mismatch #{key} #{data[key].class}!=#{value.class}"
      end
    end
    data[key] = value
  end
end

#update_options(options) ⇒ Object



87
88
89
90
91
92
# File 'app/models/breeze/view_base.rb', line 87

def update_options( options )
  return unless options
  option_definitions.each do |option|
    set_option(option.name,  options[option.name])
  end
end