Class: RearInput

Inherits:
Object
  • Object
show all
Includes:
RearConstants, RearUtils
Defined in:
lib/rear/input.rb

Constant Summary

Constants included from RearConstants

RearConstants::ASSETS__PATH, RearConstants::ASSETS__SUFFIX, RearConstants::ASSETS__SUFFIX_REGEXP, RearConstants::ASSOCS__STRUCT, RearConstants::COLUMNS__BOOLEAN_MAP, RearConstants::COLUMNS__DEFAULT_TYPE, RearConstants::COLUMNS__HANDLED_TYPES, RearConstants::COLUMNS__PANE_MAX_LENGTH, RearConstants::FILTERS__DECORATIVE_CMP, RearConstants::FILTERS__DEFAULT_TYPE, RearConstants::FILTERS__HANDLED_TYPES, RearConstants::FILTERS__QUERY_MAP, RearConstants::FILTERS__STR_TO_BOOLEAN, RearConstants::PAGER__SIDE_PAGES, RearConstants::PATH__TEMPLATES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RearUtils

ar?, associated_model_controller, dm?, dom_id_generator, extract_ar_assocs, extract_ar_columns, extract_associated_ar_model, extract_assocs, extract_columns, extract_constant, extract_dm_assocs, extract_dm_columns, extract_sq_assocs, extract_sq_columns, initialize_model_controller, is_orm?, normalize_html_attrs, number_with_delimiter, orm, quote_ar_column, quote_column, quote_dm_column, quote_sq_column, sq?

Constructor Details

#initialize(name, type = COLUMNS__DEFAULT_TYPE, attrs = {}, brand_new_item = nil, &proc) ⇒ RearInput

Returns a new instance of RearInput.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rear/input.rb', line 8

def initialize name, type = COLUMNS__DEFAULT_TYPE, attrs = {}, brand_new_item = nil, &proc
  @name, @string_name, @type = name, name.to_s, type
  @brand_new_item = brand_new_item
  @dom_id = ['Rear', 'Input', 'DOMIDFor', @string_name.capitalize.gsub(/\W/, ''), __id__].join
  @css_class = 'CSSClassFor' << @dom_id

  @pane_template = pane_template @type
  @pane_value    = proc do
    val = item[column.name]
    if val.is_a?(String)
      val.size > COLUMNS__PANE_MAX_LENGTH ?
        val[0..COLUMNS__PANE_MAX_LENGTH] + ' ...' : val
    else
      val
    end
  end

  @editor_template = editor_template @type
  @editor_value    = proc { item[column.name] }

  @active_options  = proc { item[column.name] }

  @html_attrs = Hash[[:*, :pane, :editor].map {|s| [s, {}]}]
  html_attrs(attrs)
  self.instance_exec(&proc) if proc
end

Instance Attribute Details

#css_classObject (readonly)

Returns the value of attribute css_class.



6
7
8
# File 'lib/rear/input.rb', line 6

def css_class
  @css_class
end

#dom_idObject (readonly)

Returns the value of attribute dom_id.



6
7
8
# File 'lib/rear/input.rb', line 6

def dom_id
  @dom_id
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/rear/input.rb', line 5

def name
  @name
end

#string_nameObject (readonly)

Returns the value of attribute string_name.



5
6
7
# File 'lib/rear/input.rb', line 5

def string_name
  @string_name
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/rear/input.rb', line 5

def type
  @type
end

Instance Method Details

#active_optionsObject



173
174
175
# File 'lib/rear/input.rb', line 173

def active_options
  @active_options
end

#boolean?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/rear/input.rb', line 231

def boolean?
  type == :boolean
end

#checkbox?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/rear/input.rb', line 215

def checkbox?
  type == :checkbox
end

#ckeditor(opts = {}) ⇒ Object

various opts for CKEditor

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :path (Object)

    physical path to folder containing images/videos to be picked up by file browser

  • :prefix (Object)

    file browser will build URL to file/video by extracting path(set via ‘:path` option) from full path to file. that’s it, if path is “/foo/bar” and full path to file is “/foo/bar/baz/image.jpg”, the URL used in browser will be “/baz/image.jpg”. ‘:prefix` option allow to define a string to be prepended to “/baz/image.jpg”.

  • :lang (Object)

    localizing CKEditors



312
313
314
# File 'lib/rear/input.rb', line 312

def ckeditor opts = {}
  @ckeditor_opts = opts
end

#ckeditor_optsObject



315
# File 'lib/rear/input.rb', line 315

def ckeditor_opts; @ckeditor_opts || {}; end

#disable!Object Also known as: disabled!



198
199
200
# File 'lib/rear/input.rb', line 198

def disable!
  @disabled = true
end

#disabled?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/rear/input.rb', line 203

def disabled?
  @disabled
end

#editor?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/rear/input.rb', line 185

def editor?
  @editor_template
end

#editor_attrs(attrs = {}) ⇒ Object

Note:

will override any attrs set globally via ‘editor_attrs` or `html_attrs` at class level

set HTML attributes to be used on current column on editor pages



76
77
78
79
# File 'lib/rear/input.rb', line 76

def editor_attrs attrs = {}
  set_html_attrs(attrs, :editor) if attrs.any?
  editor_attrs? || html_attrs
end

#editor_attrs?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/rear/input.rb', line 81

def editor_attrs?
  (a = @html_attrs[:editor]) && a.any? && a
end

#editor_template(template = nil, &proc) ⇒ Object Also known as: editor



102
103
104
105
106
107
108
109
110
# File 'lib/rear/input.rb', line 102

def editor_template template = nil, &proc
  if template
    @editor_template = 'editor/%s.slim' % template
  elsif template == false
    @editor_template = nil
  end
  @editor_template = proc if proc
  @editor_template
end

#editor_value(&proc) ⇒ Object



113
114
115
116
# File 'lib/rear/input.rb', line 113

def editor_value &proc
  @editor_value = proc if proc
  @editor_value
end

#html_attrs(attrs = {}) ⇒ Object Also known as: attrs

Note:

will override any attrs set globally via ‘html_attrs` at class level

set HTML attributes to be used on current column on both pane and editor pages



57
58
59
60
# File 'lib/rear/input.rb', line 57

def html_attrs attrs = {}
  set_html_attrs(attrs) if attrs.any?
  @html_attrs[:*] || {}
end

#label(label = nil) ⇒ Object



41
42
43
44
# File 'lib/rear/input.rb', line 41

def label label = nil
  @label = label if label
  @label
end

#multiple!Object



207
208
209
# File 'lib/rear/input.rb', line 207

def multiple!
  @multiple = true
end

#multiple?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/rear/input.rb', line 211

def multiple?
  @multiple
end

#name?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/rear/input.rb', line 35

def name?
  return if disabled?
  return if readonly?
  multiple? || checkbox? ? '%s[]' % name : name.to_s
end

#optioned?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/rear/input.rb', line 177

def optioned?
  select? || checkbox? || radio?
end

#options(*args, &proc) ⇒ Object

Note:

when using :checkbox type or :select type with :multiple option, Espresso will send an Array of keys to your ORM. usually ORM will handle received array automatically, however, if you want to send a string rather than array, use ‘before` callback to coerce the array into string.

required on :radio, :checkbox and :select columns. options provided as Array or Hash use an Array when keys are the same as values. use a Hash when keys are different from values. if block given it should return an Array of selected keys or a single key. if block not given, no options will be selected.

before :save do
  params[:fruit] = params[:fruit].join(',')
end
column :fruit, :select, :multiple => true do
  options('Apple', 'Orange', 'Peach') { ['Orange', 'Peach'] }
end

Examples:

use an Array. will send ‘Orange’ to ORM


column :fruit, :select do
  options('Apple', 'Orange', 'Peach') { 'Orange' }
end

use a Hash. will send ‘2’ to ORM


column :fruit, :select do
  options(1 => 'Apple', 2 => 'Orange', 3 => 'Peach') { 2 }
end

this example will send [‘Orange’, ‘Peach’] Array

which will be caught by `before :save` callback
and coerced into string

will send [‘2’, ‘3’] Array to your ORM

column :fruit, :checkbox do
  options(1 => 'Apple', 2 => 'Orange', 3 => 'Peach') { [2, 3] }
end


165
166
167
168
169
170
171
# File 'lib/rear/input.rb', line 165

def options *args, &proc
  return @options || {} if args.empty?
  @options = args.inject({}) do |f,c|
    c.is_a?(Hash) ? f.merge(c) : f.merge(c => c)
  end
  @active_options = proc if proc
end

#order_by(*columns) ⇒ Object

Note:

do not pass ordering vector when setting costom ‘order_by` for columns. vector will be added automatically, so pass only column names. if vector passed, ordering will broke badly.

when ordering by some column, “ORDER BY” will use only the selected column. this column-specific setup allow to order returned items by multiple columns.

# @example

class News
  include DataMapper::Resource

  property :id, Serial
  property :name, String
  property :date, Date
  property :status, Integer
end

Rear.register News do
  input :date do
    order_by :date, :id
  end
end

this method are also useful when you need to sort items by a “decorative” column, meant a column that does not exists in db but you need it on pane pages to render more informative rows. For ex. display category of each article when rendering articles

# @example

class Article
  include DataMapper::Resource
  # ...

  belongs_to :category
end

Rear.register do
  # defining a "decorative" column to display categories of each article
  input :Categories do
    pane { item.categories.map {|c| c.name}.join(', ') }
    # when Categories column clicked on pane page,
    # we want articles to be sorted by category id
    order_by :category_id
  end
end


281
282
283
284
# File 'lib/rear/input.rb', line 281

def order_by *columns
  @order_by = columns if columns.any?
  @order_by || [name]
end

#order_by?Boolean

Returns:

  • (Boolean)


286
287
288
# File 'lib/rear/input.rb', line 286

def order_by?
  @order_by
end

#pane?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/rear/input.rb', line 181

def pane?
  @pane_template
end

#pane_attrs(attrs = {}) ⇒ Object

Note:

will override any attrs set globally via ‘pane_attrs` or `html_attrs` at class level

set HTML attributes to be used on current column on pane pages



65
66
67
68
# File 'lib/rear/input.rb', line 65

def pane_attrs attrs = {}
  set_html_attrs(attrs, :pane) if attrs.any?
  pane_attrs? || html_attrs
end

#pane_attrs?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rear/input.rb', line 70

def pane_attrs?
  (a = @html_attrs[:pane]) && a.any? && a
end

#pane_template(template = nil, &proc) ⇒ Object Also known as: pane



85
86
87
88
89
90
91
92
93
94
# File 'lib/rear/input.rb', line 85

def pane_template template = nil, &proc
  caller.each {|l| puts l} if template == :integer
  if template
    @pane_template = 'pane/%s.slim' % template
  elsif template == false
    @pane_template = nil
  end
  @pane_template = proc if proc
  @pane_template
end

#pane_value(&proc) ⇒ Object



97
98
99
100
# File 'lib/rear/input.rb', line 97

def pane_value &proc
  @pane_value = proc if proc
  @pane_value
end

#radio?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/rear/input.rb', line 223

def radio?
  type == :radio
end

#readonly!Object



189
190
191
# File 'lib/rear/input.rb', line 189

def readonly!
  @readonly = true
end

#readonly?Boolean

Returns:

  • (Boolean)


193
194
195
196
# File 'lib/rear/input.rb', line 193

def readonly?
  return if @brand_new_item
  @readonly
end

#row(row = nil) ⇒ Object



46
47
48
49
# File 'lib/rear/input.rb', line 46

def row row = nil
  @row = row.to_s if row
  @row 
end

#row?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/rear/input.rb', line 51

def row?
  @row
end

#select?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/rear/input.rb', line 219

def select?
  type == :select
end

#snippets(*snippets, &proc) ⇒ Object

allow to define a list of snippets you need to insert into edited content. relevant only on :ace / :ckeditor columns.



292
293
294
295
296
# File 'lib/rear/input.rb', line 292

def snippets *snippets, &proc
  snippets.any? && @snippets = snippets
  proc && @snippets = proc
  @snippets
end

#textual?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/rear/input.rb', line 227

def textual?
  type == :text || type == :rte
end

#value(&proc) ⇒ Object



118
119
120
121
# File 'lib/rear/input.rb', line 118

def value &proc
  pane_value   &proc
  editor_value &proc
end