Class: Sinatra::XeditableAbstract

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/xeditable_abstract.rb

Overview

Common methods for Xeditable related classes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, url, html_class) ⇒ XeditableAbstract

Returns a new instance of XeditableAbstract.



47
48
49
50
51
52
53
54
# File 'lib/sinatra/xeditable_abstract.rb', line 47

def initialize(values, url, html_class)
  process_values(values)
  @url_data = url
  @html_class = html_class
  @title = ::I18n.t(:Select_an_option)
  @method = "put"
  @active = true
end

Instance Attribute Details

#activeObject

By default, true. Could be set to false conditional on permission



45
46
47
# File 'lib/sinatra/xeditable_abstract.rb', line 45

def active
  @active
end

#html_classObject (readonly)

Class of the text that links to the editable select. Should be unique for every type of select.



39
40
41
# File 'lib/sinatra/xeditable_abstract.rb', line 39

def html_class
  @html_class
end

#methodObject

Method to send the request. By default, is put



43
44
45
# File 'lib/sinatra/xeditable_abstract.rb', line 43

def method
  @method
end

#titleObject

Title of dialog



41
42
43
# File 'lib/sinatra/xeditable_abstract.rb', line 41

def title
  @title
end

#url_dataObject (readonly)

Url to send information to



37
38
39
# File 'lib/sinatra/xeditable_abstract.rb', line 37

def url_data
  @url_data
end

#valuesObject (readonly)

Hash of key, values to put on select



35
36
37
# File 'lib/sinatra/xeditable_abstract.rb', line 35

def values
  @values
end

Instance Method Details

#javascriptObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sinatra/xeditable_abstract.rb', line 65

def javascript
  source = values.map {|v|
    "{value:'#{v[0]}', text:'#{v[1].gsub("'", "")}'}"
  }.join(",\n")

  "
$(document).ready(function () {
$('.#{html_class}').editable({
type:'#{xeditable_type}',
title:'#{title}',
mode:'inline',
ajaxOptions: {
type: '#{method}'
},
url:'#{url_data}',
source: [#{source}]
});
});
"
end

#process_values(x) ⇒ Object



58
59
60
61
62
63
# File 'lib/sinatra/xeditable_abstract.rb', line 58

def process_values(x)
  @values = {}
  x.each_pair {|key, val|
    @values[key.to_s] = val
  }
end

#xeditable_typeObject



55
56
57
# File 'lib/sinatra/xeditable_abstract.rb', line 55

def xeditable_type
  raise "Should be defined xeditable_type for javascript"
end