Module: Togo::DataMapper::Model::ClassMethods

Defined in:
lib/togo/model/model.rb

Instance Method Summary collapse

Instance Method Details

#configure_property(property, opts = {}) ⇒ Object



41
42
43
44
# File 'lib/togo/model/model.rb', line 41

def configure_property(property,opts = {})
  custom_template_for(property, opts.delete(:template)) if opts.has_key?(:template)
  class_variable_get(:@@property_options).merge!(property => opts)
end

#create_content!(attrs) ⇒ Object



56
57
58
# File 'lib/togo/model/model.rb', line 56

def create_content!(attrs)
  stage_content(new,attrs).save
end

#delete_content(content) ⇒ Object



70
71
72
# File 'lib/togo/model/model.rb', line 70

def delete_content(content)
  content.destroy!
end

#display_nameObject



74
75
76
# File 'lib/togo/model/model.rb', line 74

def display_name
  name.gsub(/([a-z])([A-Z])/,"\\1 \\2").pluralize
end

#field_class_for(property) ⇒ Object



98
99
100
# File 'lib/togo/model/model.rb', line 98

def field_class_for(property)
  property.type
end

#form_for(property, content, opts = {}) ⇒ Object

Display the form template for a property



47
48
49
50
# File 'lib/togo/model/model.rb', line 47

def form_for(property,content,opts = {})
  template = class_variable_get(:@@custom_form_templates)[property.name] || File.join(File.dirname(__FILE__),'types',"#{property.type}.erb")
  Erubis::TinyEruby.new(File.open(template).read).result(binding)
end

#form_properties(*args) ⇒ Object

Let the user determine what properties to show in form view



37
38
39
# File 'lib/togo/model/model.rb', line 37

def form_properties(*args)
  class_variable_set(:@@user_form_properties, args.to_a)
end

#get_form_propertiesObject



106
107
108
# File 'lib/togo/model/model.rb', line 106

def get_form_properties
  pick_properties(:form, class_variable_get(:@@user_form_properties))
end

#get_list_propertiesObject



102
103
104
# File 'lib/togo/model/model.rb', line 102

def get_list_properties
  pick_properties(:list, class_variable_get(:@@user_list_properties))
end

#list_properties(*args) ⇒ Object

Let the user determine what properties to show in list view



32
33
34
# File 'lib/togo/model/model.rb', line 32

def list_properties(*args)
  class_variable_set(:@@user_list_properties, args.to_a)
end

#property_optionsObject



78
79
80
# File 'lib/togo/model/model.rb', line 78

def property_options
  class_variable_get(:@@property_options)          
end

#search(opts) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/togo/model/model.rb', line 82

def search(opts)
  q = "%#{opts[:q].gsub(/\s+/,'%')}%"
  limit = opts[:limit]
  offset = opts[:offset]
  order = opts[:order]
  conditions, values = [], []
  search_properties.each{|l|
    conditions << "#{l.name} like ?"
    values << q
  }
  params = {:conditions => [conditions.join(' OR ')] + values}
  params.merge!(:limit => limit.to_i, :offset => offset.to_i) if limit and offset
  params.merge!(:order => order) if order
  all(params)
end

#stage_content(content, attrs) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/togo/model/model.rb', line 60

def stage_content(content,attrs)
  content.attributes = properties.inject({}){|m,p| attrs[p.name.to_sym] ? m.merge!(p.name.to_sym => attrs[p.name.to_sym]) : m}
  relationships.each do |r|
    val = attrs["related_#{r.name}".to_sym]
    next if not val or val == 'unset'
    content = RelationshipManager.create(content, r, :ids => val).relate
  end
  content
end

#update_content!(id, attrs) ⇒ Object



52
53
54
# File 'lib/togo/model/model.rb', line 52

def update_content!(id,attrs)
  stage_content(first(:id => id),attrs).save
end