Module: MerbAdmin::AbstractModel::SequelSupport
- Defined in:
- lib/sequel_support.rb
Defined Under Namespace
Modules: InstanceMethods
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #associations ⇒ Object
- #belongs_to_associations ⇒ Object
- #count(options = {}) ⇒ Object
- #create(params = {}) ⇒ Object
- #destroy_all! ⇒ Object
- #first(options = {}) ⇒ Object
- #get(id) ⇒ Object
- #has_many_associations ⇒ Object
- #has_one_associations ⇒ Object
- #new(params = {}) ⇒ Object
- #paginated(options = {}) ⇒ Object
- #properties ⇒ Object
Instance Method Details
#all(options = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/sequel_support.rb', line 49 def all( = {}) offset = .delete(:offset) limit = .delete(:limit) sort = .delete(:sort) || :id sort_order = .delete(:sort_reverse) ? :desc : :asc if [:conditions] && ![:conditions].empty? model.where([:conditions]).order(sort.to_sym.send(sort_order)) else model.order(sort.to_sym.send(sort_order)) end end |
#associations ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/sequel_support.rb', line 108 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_associations ⇒ Object
102 103 104 105 106 |
# File 'lib/sequel_support.rb', line 102 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( = {}) if [:conditions] && ![:conditions].empty? model.where([:conditions]).count else model.count end end |
#create(params = {}) ⇒ Object
78 79 80 |
# File 'lib/sequel_support.rb', line 78 def create(params = {}) model.create(params).extend(InstanceMethods) end |
#destroy_all! ⇒ Object
86 87 88 |
# File 'lib/sequel_support.rb', line 86 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( = {}) sort = .delete(:sort) || :id sort_order = .delete(:sort_reverse) ? :desc : :asc if [:conditions] && ![:conditions].empty? model.order(sort.to_sym.send(sort_order)).first([: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_associations ⇒ Object
90 91 92 93 94 |
# File 'lib/sequel_support.rb', line 90 def has_many_associations associations.select do |association| association[:type] == :has_many end end |
#has_one_associations ⇒ Object
96 97 98 99 100 |
# File 'lib/sequel_support.rb', line 96 def has_one_associations associations.select do |association| association[:type] == :has_one end end |
#new(params = {}) ⇒ Object
82 83 84 |
# File 'lib/sequel_support.rb', line 82 def new(params = {}) model.new(params).extend(InstanceMethods) end |
#paginated(options = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sequel_support.rb', line 63 def paginated( = {}) page = .delete(:page) || 1 per_page = .delete(:per_page) || MerbAdmin[:per_page] page_count = (count().to_f / per_page).ceil sort = .delete(:sort) || :id sort_order = .delete(:sort_reverse) ? :desc : :asc if [:conditions] && ![:conditions].empty? [page_count, model.paginate(page.to_i, per_page).where([: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 |
#properties ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/sequel_support.rb', line 122 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 |