Class: Orange::Scaffold

Inherits:
Resource show all
Defined in:
lib/orange-core/resources/scaffold.rb

Instance Method Summary collapse

Methods inherited from Resource

call_me, #do_view, #find_extras, #init, #initialize, #nests, #options, #orange, #orange_name, #routable, #set_orange, set_orange, #view, #view_opts, #viewable, viewable

Methods included from ClassInheritableAttributes

#cattr_accessor, #cattr_reader, #cattr_writer, eval_in_accessor_module, fetch_value, store_value

Constructor Details

This class inherits a constructor from Orange::Resource

Instance Method Details

#add_scaffold_type(type, &block) ⇒ Object



26
27
28
# File 'lib/orange-core/resources/scaffold.rb', line 26

def add_scaffold_type(type, &block)
  @scaffold_types[type] = Proc.new
end

#afterLoadObject

Load the scaffold helpers



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/orange-core/resources/scaffold.rb', line 6

def afterLoad
  orange.add_pulp Pulp::ScaffoldHelpers
  Packet.meta_methods(/view_([a-zA-Z_]+)/) do |packet, match, args|
    model = args.shift
    args = args.extract_with_defaults(:mode => match[1].to_sym)
    packet.view(model, args)
  end
  @scaffold_types = {}
  add_scaffold_type(:boolean) do |name, val, opts|
    opts = opts.with_defaults({:wrap_tag => 'p'})
    if opts[:show]
      val ? "true" : "false"
    else
      ret = "<input type='hidden' name='#{opts[:model_name]}[#{name}]' value='0' /><input type='checkbox' name='#{opts[:model_name]}[#{name}]' value='1' #{'checked="checked"' if (val && val != '')}/>"
      ret = "<label for=''>#{opts[:display_name]}</label><br />" + ret if opts[:label]
      ret = "<#{opts[:wrap_tag]}>#{ret}</#{opts[:wrap_tag]}>" if opts[:wrap_tag]
    end
  end
end

#scaffold_attribute(packet, prop, model_name, *args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/orange-core/resources/scaffold.rb', line 30

def scaffold_attribute(packet, prop, model_name, *args)
  args = args.extract_options!
  args = args.with_defaults({:packet => packet, :value => '', :label => false, :show => false, :wrap_tag => 'p'})
  val = args[:value]
  label = args[:label]
  show = args[:show]
  name = prop[:name]
  human_readable_name = name.to_s.split('_').each{|w| w.capitalize!}.join(' ')
  relationship = prop[:relationship]
  display_name = prop[:display_name] || human_readable_name
  return @scaffold_types[prop[:type]].call(name, val, args.with_defaults!(:display_name => display_name, :model_name => model_name)) if @scaffold_types.has_key?(prop[:type])
  unless show
    case prop[:type]
    when :title
      val.gsub!('"', '&quot;')
      ret = "<input class=\"title\" type=\"text\" value=\"#{val}\" name=\"#{model_name}[#{name}]\" />"
    when :text
      val.gsub!('"', '&quot;')
      ret = "<input type=\"text\" value=\"#{val}\" name=\"#{model_name}[#{name}]\" />"
    when :fulltext
      ret = "<textarea name='#{model_name}[#{name}]'>#{val}</textarea>"
    when :boolean
      human_readable_name = human_readable_name + '?'
      ret = "<input type='hidden' name='#{model_name}[#{name}]' value='0' /><input type='checkbox' name='#{model_name}[#{name}]' value='1' #{'checked="checked"' if (val && val != '')}/>"
    when :date
      val.gsub!('"', '&quot;')
      ret = "<input class=\"date\" type=\"text\" value=\"#{val}\" name=\"#{model_name}[#{name}]\" />"
    when :belongs
      val_id = val.blank? ? nil : val.id
      vals = Object.const_get(prop[:related_to]).all
      vals = vals.map{|obj| "<option value=\"#{obj.id}\"#{val_id && val_id == obj.id ? 'selected=\'selected\'' : ''}>#{obj.scaffold_name}</option>"}.join("\n")
      ret = "<select name=\"#{model_name}[#{name}_id]\">#{prop[:required] ? '' : '<option value="">None</option>'}#{vals}</select>"
    when :has_one
      ret = ""
      args[:wrap_tag] = false
    when :has_many
      ret = ""
      args[:wrap_tag] = false
    when :has_and_belongs_to_many
      ret = ""
      args[:wrap_tag] = false
    when :number
      val = val.to_s
      ret = "<input class=\"number\" type=\"text\" value=\"#{val}\" name=\"#{model_name}[#{name}]\"  />"
    else
      val.gsub!('"', '&quot;')
      ret = "<input type=\"text\" value=\"#{val}\" name=\"#{model_name}[#{name}]\" />"
    end
    ret = "<label for=''>#{display_name}</label><br />" + ret if label && !ret.blank?
    ret = "<#{args[:wrap_tag]}>#{ret}</#{args[:wrap_tag]}>" if args[:wrap_tag]
  else
    case prop[:type]
    when :title
      ret = "<h1 class='#{model_name}-#{name}'>#{val}</h3>"
    when :text
      ret = "<p class='#{model_name}-#{name}'>#{val}</p>"
    when :fulltext
      ret = "<div class='#{model_name}-#{name}'>#{val}</div>"
    else
      ret = "<div class='#{model_name}-#{name}'>#{val}</div>"
    end
  end
  return ret
end