Module: RightRails::JavaScriptGenerator::Methods

Included in:
RightRails::JavaScriptGenerator
Defined in:
lib/right_rails/java_script_generator.rb

Overview

This module contains the predefined methods collection

Instance Method Summary collapse

Instance Method Details

#<<(code) ⇒ Object

just pushes a line of code into the thread



41
42
43
44
# File 'lib/right_rails/java_script_generator.rb', line 41

def << (code)
  @util.write(code)
  self
end

#[](record_or_id) ⇒ Object

referring an element by an id or a record



36
37
38
# File 'lib/right_rails/java_script_generator.rb', line 36

def [](record_or_id)
  @util.record("#{RightRails::Helpers.prefix}$(\"#{@util.dom_id(record_or_id)}\")")
end

#documentObject



61
62
63
# File 'lib/right_rails/java_script_generator.rb', line 61

def document
  @util.record('$(document)')
end

#find(css_rule) ⇒ Object

builds a css-select block



47
48
49
# File 'lib/right_rails/java_script_generator.rb', line 47

def find(css_rule)
  @util.record("#{RightRails::Helpers.prefix}$$(\"#{css_rule}\")")
end

#get(name) ⇒ Object

access to the javascript variables



52
53
54
# File 'lib/right_rails/java_script_generator.rb', line 52

def get(name)
  @util.record(name)
end

#insert(record, position = nil) ⇒ Object

builds the record HTML code and then insterts it in place



82
83
84
# File 'lib/right_rails/java_script_generator.rb', line 82

def insert(record, position=nil)
  self.RR.insert(*[record.class.table_name, @util.render(record), position].compact)
end

#insert_and_care(record, position = nil) ⇒ Object

generates a script that inserts the record partial, then updates the form and the flashes block



87
88
89
90
91
92
# File 'lib/right_rails/java_script_generator.rb', line 87

def insert_and_care(record, position=nil)
  insert(record, position)
  @util.template.instance_variable_set("@#{record.class.table_name.singularize}", record.class.new)
  replace_form_for(record.class.new)
  update_flash
end

#redirect_to(location) ⇒ Object

generates the redirection script



70
71
72
73
# File 'lib/right_rails/java_script_generator.rb', line 70

def redirect_to(location)
  self.get(:document)[:location].href = (location.is_a?(String) ? location : @util.template.url_for(location))
  self
end

#reloadObject

generates the page reload script



76
77
78
79
# File 'lib/right_rails/java_script_generator.rb', line 76

def reload
  self.get(:document)[:location].reload
  self
end

#remove(record) ⇒ Object

removes the record element from the page



106
107
108
# File 'lib/right_rails/java_script_generator.rb', line 106

def remove(record)
  self.RR.remove(@util.dom_id(record))
end

#replace(record) ⇒ Object

replaces the record element on the page



95
96
97
# File 'lib/right_rails/java_script_generator.rb', line 95

def replace(record)
  self.RR.replace(@util.dom_id(record), @util.render(record))
end

#replace_and_care(record) ⇒ Object

replaces the record partial and updates the flash



100
101
102
103
# File 'lib/right_rails/java_script_generator.rb', line 100

def replace_and_care(record)
  replace(record)
  update_flash
end

#replace_form_for(record) ⇒ Object

renders and updates a form for the record



116
117
118
# File 'lib/right_rails/java_script_generator.rb', line 116

def replace_form_for(record)
  self.RR.replace_form(@util.form_id_for(record), @util.render('form'))
end

#set(name, value) ⇒ Object

variables initializing method



57
58
59
# File 'lib/right_rails/java_script_generator.rb', line 57

def set(name, value)
  @util.record("var #{name}=#{@util.to_js_type(value)}")
end

#show_form_for(record) ⇒ Object

renders and shows a form for the record



111
112
113
# File 'lib/right_rails/java_script_generator.rb', line 111

def show_form_for(record)
  self.RR.show_form_for(@util.dom_id(record), @util.render('form'))
end

#update_flash(content = nil) ⇒ Object

updates the flashes block



121
122
123
124
# File 'lib/right_rails/java_script_generator.rb', line 121

def update_flash(content=nil)
  self.RR.update_flash(content || @util.template.flashes)
  @util.template.flash.clear
end

#windowObject



65
66
67
# File 'lib/right_rails/java_script_generator.rb', line 65

def window
  @util.record('$(window)')
end