Class: RearORM

Inherits:
Object
  • Object
show all
Includes:
RearUtils
Defined in:
lib/rear/orm.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(model, pkey = :id) ⇒ RearORM

Returns a new instance of RearORM.



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

def initialize model, pkey = :id
  @model, @pkey, @orm = model, pkey, orm(model)
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/rear/orm.rb', line 4

def model
  @model
end

#pkeyObject (readonly)

Returns the value of attribute pkey.



4
5
6
# File 'lib/rear/orm.rb', line 4

def pkey
  @pkey
end

Instance Method Details

#[](id) ⇒ Object



10
11
12
13
14
# File 'lib/rear/orm.rb', line 10

def [] id
  sequel? ?
    model[id] :
    model.first(conditions: {pkey => id})
end

#assoc_count(assoc, item, conditions) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rear/orm.rb', line 41

def assoc_count assoc, item, conditions
  if sequel?
    sequel_dataset(item.send('%s_dataset' % assoc), conditions).count
  else
    if result = item.send(assoc)
      result.respond_to?(:count) ? result.count(conditions) : 1
    else
      0
    end
  end
end

#assoc_filter(assoc, item, conditions) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/rear/orm.rb', line 32

def assoc_filter assoc, item, conditions
  if sequel?
    sequel_dataset(item.send('%s_dataset' % assoc), conditions).all
  else
    result = item.send(assoc, conditions)
    result.respond_to?(:size) ? result : [result].compact
  end
end

#count(conditions = {}) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/rear/orm.rb', line 16

def count conditions = {}
  if sequel?
    sequel_dataset(model, conditions).count
  else
    model.count(conditions)
  end
end

#delete_multiple(*ids) ⇒ Object



53
54
55
56
57
58
# File 'lib/rear/orm.rb', line 53

def delete_multiple *ids
  ids.flatten!
  model.destroy(ids) if @orm == :ar
  model.all(pkey => ids).destroy! if @orm == :dm
  model.filter(pkey => ids).destroy if @orm == :sq
end

#filter(conditions = {}) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/rear/orm.rb', line 24

def filter conditions = {}
  if sequel?
    sequel_dataset(model, conditions).all
  else
    model.all(conditions)
  end
end

#sequel?Boolean

Returns:

  • (Boolean)


60
# File 'lib/rear/orm.rb', line 60

def sequel?; @orm == :sq end

#sequel_dataset(dataset, conditions = {}) ⇒ Object



62
63
64
65
66
67
# File 'lib/rear/orm.rb', line 62

def sequel_dataset dataset, conditions = {}
  filters, limit, offset, order =
    conditions.values_at(:conditions, :limit, :offset, :order)
  ds = limit ? dataset.limit(*[limit, offset].compact) : dataset
  ds.filter(filters || {}).order(*[order].compact)
end

#singularize(smth) ⇒ Object



69
70
71
# File 'lib/rear/orm.rb', line 69

def singularize smth
  model.send(:singularize, smth) if sequel?
end