Module: MerbAdmin::AbstractModel::SequelSupport

Defined in:
lib/sequel_support.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#all(options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sequel_support.rb', line 60

def all(options = {})
  offset = options.delete(:offset)
  limit = options.delete(:limit)

  sort = options.delete(:sort) || :id
  sort_order = options.delete(:sort_reverse) ? :desc : :asc

  if options[:conditions] && !options[:conditions].empty?
    model.where(options[:conditions]).order(sort.to_sym.send(sort_order))
  else
    model.order(sort.to_sym.send(sort_order))
  end
end

#associationsObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/sequel_support.rb', line 119

def associations
  model.all_association_reflections.map do |association|
    {
      :name => association_name_lookup(association),
      :pretty_name => association_pretty_name_lookup(association),
      :type => association_type_lookup(association),
      :parent_model => association_parent_model_lookup(association),
      :parent_key => association_parent_key_lookup(association),
      :child_model => association_child_model_lookup(association),
      :child_key => association_child_key_lookup(association),
    }
  end
end

#belongs_to_associationsObject



113
114
115
116
117
# File 'lib/sequel_support.rb', line 113

def belongs_to_associations
  associations.select do |association|
    association[:type] == :belongs_to
  end
end

#count(options = {}) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/sequel_support.rb', line 30

def count(options = {})
  if options[:conditions] && !options[:conditions].empty?
    model.where(options[:conditions]).count
  else
    model.count
  end
end

#create(params = {}) ⇒ Object



89
90
91
# File 'lib/sequel_support.rb', line 89

def create(params = {})
  model.create(params).extend(InstanceMethods)
end

#destroy_all!Object



97
98
99
# File 'lib/sequel_support.rb', line 97

def destroy_all!
  model.destroy
end

#first(options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/sequel_support.rb', line 38

def first(options = {})
  sort = options.delete(:sort) || :id
  sort_order = options.delete(:sort_reverse) ? :desc : :asc

  if options[:conditions] && !options[:conditions].empty?
    model.order(sort.to_sym.send(sort_order)).first(options[:conditions]).extend(InstanceMethods)
  else
    model.order(sort.to_sym.send(sort_order)).first.extend(InstanceMethods)
  end
end

#get(id) ⇒ Object



26
27
28
# File 'lib/sequel_support.rb', line 26

def get(id)
  model.first(:id => id).extend(InstanceMethods)
end

#has_many_associationsObject



101
102
103
104
105
# File 'lib/sequel_support.rb', line 101

def has_many_associations
  associations.select do |association|
    association[:type] == :has_many
  end
end

#has_one_associationsObject



107
108
109
110
111
# File 'lib/sequel_support.rb', line 107

def has_one_associations
  associations.select do |association|
    association[:type] == :has_one
  end
end

#last(options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/sequel_support.rb', line 49

def last(options = {})
  sort = options.delete(:sort) || :id
  sort_order = options.delete(:sort_reverse) ? :desc : :asc

  if options[:conditions] && !options[:conditions].empty?
    model.order(sort.to_sym.send(sort_order)).last(options[:conditions]).extend(InstanceMethods)
  else
    model.order(sort.to_sym.send(sort_order)).last.extend(InstanceMethods)
  end
end

#new(params = {}) ⇒ Object



93
94
95
# File 'lib/sequel_support.rb', line 93

def new(params = {})
  model.new(params).extend(InstanceMethods)
end

#paginated(options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sequel_support.rb', line 74

def paginated(options = {})
  page = options.delete(:page) || 1
  per_page = options.delete(:per_page) || MerbAdmin[:per_page]
  page_count = (count(options).to_f / per_page).ceil

  sort = options.delete(:sort) || :id
  sort_order = options.delete(:sort_reverse) ? :desc : :asc

  if options[:conditions] && !options[:conditions].empty?
    [page_count, model.paginate(page.to_i, per_page).where(options[:conditions]).order(sort.to_sym.send(sort_order))]
  else
    [page_count, model.paginate(page.to_i, per_page).order(sort.to_sym.send(sort_order))]
  end
end

#propertiesObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/sequel_support.rb', line 133

def properties
  model.columns.map do |property|
    {
      :name => property,
      :pretty_name => property.to_s.gsub(/_id$/, "").gsub("_", " ").capitalize,
      :type => property_type_lookup(property),
      :length => property_length_lookup(property),
      :nullable? => model.db_schema[property][:allow_null],
      :serial? => model.db_schema[property][:primary_key],
    }
  end
end