Class: Skyline::Editors::JoinableList

Inherits:
Editor
  • Object
show all
Defined in:
app/helpers/skyline/editors/joinable_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(names, record, field, template) ⇒ JoinableList

Returns a new instance of JoinableList.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/skyline/editors/joinable_list.rb', line 5

def initialize(names,record,field,template)
  super
  RAILS_DEFAULT_LOGGER.debug("@attribute => #{@attribute_names.inspect}")

  @reflection = self.field.reflection
  if @reflection.macro == :has_many
    raise "JoinableList can only be used with HABTM and has_many :through associations" unless @reflection.through_reflection
    @target_class = reflection.klass
    @proxy_class = @reflection.through_reflection.klass
  elsif @reflection.macro == :has_and_belongs_to_many
    @target_class = @reflection.klass
    @proxy_class = nil
  else
    raise "JoinableList can only be used with HABTM and has_many :through associations (was: #{@reflection.macro})"
  end
end

Instance Attribute Details

#proxy_classObject (readonly)

Returns the value of attribute proxy_class.



3
4
5
# File 'app/helpers/skyline/editors/joinable_list.rb', line 3

def proxy_class
  @proxy_class
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



3
4
5
# File 'app/helpers/skyline/editors/joinable_list.rb', line 3

def reflection
  @reflection
end

#target_classObject (readonly)

Returns the value of attribute target_class.



3
4
5
# File 'app/helpers/skyline/editors/joinable_list.rb', line 3

def target_class
  @target_class
end

Instance Method Details

#js_object_nameObject



64
65
66
# File 'app/helpers/skyline/editors/joinable_list.rb', line 64

def js_object_name
  "joinable_list_#{input_id(self.attribute_names)}"
end

#orderable?Boolean

Returns:



31
32
33
# File 'app/helpers/skyline/editors/joinable_list.rb', line 31

def orderable?
  @proxy_class && @proxy_class.respond_to?(:orderable?) && @proxy_class.orderable?
end

#output_without_errorsObject



22
23
24
25
26
27
28
29
# File 'app/helpers/skyline/editors/joinable_list.rb', line 22

def output_without_errors
  out = ""
  out << ("ul",self.rows.join("\n"),:class => "joinable-list #{"orderable" if self.orderable?}", :id => js_object_name)
  out << browse_window
  out << hidden_field_tag(input_name(self.attribute_names + ["_order"]),records.map(&:id).join(","),:id => input_id(self.attribute_names + ["order"])) if(self.orderable?)
#    out << javascript_tag("var #{js_object_name} = new JoinableList('#{js_object_name}','#{input_id(self.attribute_names + ["order"])}')")
  out
end

#proxy_id(row) ⇒ Object



52
53
54
55
56
57
58
# File 'app/helpers/skyline/editors/joinable_list.rb', line 52

def proxy_id(row)
  if @proxy_class
    row.new_record? ? random_prefix(row) : row.id
  else
    random_prefix(row)
  end
end

#random_prefix(row) ⇒ Object



60
61
62
# File 'app/helpers/skyline/editors/joinable_list.rb', line 60

def random_prefix(row)
  "n_" + row.id.to_s + "_n" + Time.now.to_i.to_s + Time.now.usec.to_s 
end

#render_row(row, proxy_id = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/helpers/skyline/editors/joinable_list.rb', line 35

def render_row(row,proxy_id = nil)
  proxy_id ||= proxy_id(row)
  edit = hidden_field_tag(input_name(self.attribute_names + [proxy_id,"_target_id"]),target_id(row),:id => input_id(self.attribute_names + [proxy_id,"_target_id"]))
  edit << delete_button(proxy_id)
  
  out = ""
  out << "<span class=\"drag\">&nbsp;</span>" if self.orderable?        
  out << ("div",edit,:class => "edit")
  out << "<div class=\"draggable\">"
  out << ("div","<h3>#{title_for(row)}</h3>", :class => "content")
  out << ("div","",:class => "clear")
  fields = content_fields(row.class,(self.attribute_names + [proxy_id]),row)
  out << ("div",fields) unless fields.blank?
  out << "</div>"    
  ("li",out,:id => row_id(proxy_id),:class => "#{cycle("odd","even")} element")
end