Module: ScaffoldingExtensions::MetaSequel

Included in:
Sequel::Model
Defined in:
lib/scaffolding_extensions/model/sequel.rb

Overview

Class methods added to Sequel::Model to allow it to work with Scaffolding Extensions.

Constant Summary collapse

SCAFFOLD_OPTIONS =
::ScaffoldingExtensions::MetaModel::SCAFFOLD_OPTIONS

Instance Method Summary collapse

Instance Method Details

#scaffold_add_associated_object(association, object, associated_object) ⇒ Object

Add the associated object to the object’s association



21
22
23
# File 'lib/scaffolding_extensions/model/sequel.rb', line 21

def scaffold_add_associated_object(association, object, associated_object)
  object.send(association_reflection(association).add_method, associated_object) unless object.send(association).include?(associated_object)
end

#scaffold_all_associationsObject

Array of all association reflections for this model



26
27
28
# File 'lib/scaffolding_extensions/model/sequel.rb', line 26

def scaffold_all_associations
  all_association_reflections
end

#scaffold_associated_class(association) ⇒ Object

The class that this model is associated with via the association



31
32
33
# File 'lib/scaffolding_extensions/model/sequel.rb', line 31

def scaffold_associated_class(association)
  association_reflection(association).associated_class
end

#scaffold_associated_objects(association, object, options) ⇒ Object

All objects that are currently associated with the given object. This method does not check that the returned associated objects meet the associated class’s scaffold_session_value constraint, as it is assumed that all objects currently assocated with the given object have already met the criteria. If that is not the case, you should override this method.



39
40
41
# File 'lib/scaffolding_extensions/model/sequel.rb', line 39

def scaffold_associated_objects(association, object, options)
  object.send(association)
end

#scaffold_association(association) ⇒ Object

The association reflection for this association



44
45
46
# File 'lib/scaffolding_extensions/model/sequel.rb', line 44

def scaffold_association(association)
  association_reflection(association)
end

#scaffold_association_type(association) ⇒ Object

The type of association, either :new for :one_to_many (as you can create new objects associated with the current object), :edit for :many_to_many (since you can edit the list of associated objects), or :one for :many_to_one.



51
52
53
54
55
56
57
58
59
60
# File 'lib/scaffolding_extensions/model/sequel.rb', line 51

def scaffold_association_type(association)
  case (a = scaffold_association(association))[:type]
    when :one_to_many
      a[:one_to_one] ? :one : :new
    when :many_to_many
      :edit
    else
      :one
  end
end

#scaffold_associationsObject

List of symbols for associations to display on the scaffolded edit page. Defaults to all associations. Can be set with an instance variable.



64
65
66
# File 'lib/scaffolding_extensions/model/sequel.rb', line 64

def scaffold_associations
  @scaffold_associations ||= associations.sort_by{|name| name.to_s}
end

#scaffold_destroy(object) ⇒ Object

Destroys the object



69
70
71
# File 'lib/scaffolding_extensions/model/sequel.rb', line 69

def scaffold_destroy(object)
  object.destroy
end

#scaffold_error_raisedObject

The error to raise, should match other errors raised by the underlying library.



74
75
76
# File 'lib/scaffolding_extensions/model/sequel.rb', line 74

def scaffold_error_raised
  Sequel::Error
end

#scaffold_fields(action = :default) ⇒ Object

Returns the list of fields to display on the scaffolded forms. Defaults to displaying all columns with the exception of the primary key column. Also includes :many_to_one associations, replacing the foriegn keys with the association itself. Can be set with an instance variable.



82
83
84
85
86
87
88
89
90
91
# File 'lib/scaffolding_extensions/model/sequel.rb', line 82

def scaffold_fields(action = :default)
  return @scaffold_fields if @scaffold_fields
  fields = columns - [primary_key]
  all_association_reflections.each do |reflection|
    next unless reflection[:type] == :many_to_one
    fields.delete(reflection[:key].to_sym)
    fields.push(reflection[:name])
  end
  @scaffold_fields = fields.sort_by{|f| f.to_s}
end

#scaffold_find_object(*args) ⇒ Object

Set *_on_save_failure = false



94
95
96
97
98
99
# File 'lib/scaffolding_extensions/model/sequel.rb', line 94

def scaffold_find_object(*args)
  obj = super
  obj.raise_on_save_failure = false
  obj.raise_on_typecast_failure = false
  obj
end

#scaffold_foreign_key(reflection) ⇒ Object

The foreign key for the given reflection



102
103
104
# File 'lib/scaffolding_extensions/model/sequel.rb', line 102

def scaffold_foreign_key(reflection)
  reflection[:key]
end

#scaffold_get_object(id) ⇒ Object

Retrieve a single model object given an id



107
108
109
# File 'lib/scaffolding_extensions/model/sequel.rb', line 107

def scaffold_get_object(id)
  self[id.to_i] || (raise scaffold_error_raised)
end

#scaffold_get_objects(options) ⇒ Object

Retrieve multiple objects given a hash of options



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/scaffolding_extensions/model/sequel.rb', line 112

def scaffold_get_objects(options)
  records = dataset
  records = records.send(scaffold_use_eager_graph ? :eager_graph : :eager, *options[:include]) if options[:include]
  records = records.order(*options[:order]) if options[:order]
  records = records.limit(options[:limit], options[:offset]) if options[:limit]
  conditions = options[:conditions]
  if conditions && Array === conditions && conditions.length > 0
    if String === conditions[0]
      records = records.filter(*conditions)
    else
      conditions.each do |cond|
        next if cond.nil?
        records = case cond
          when Hash, String then records.filter(cond)
          when Array then records.filter(*cond)
          when Proc then records.filter(&cond)
        end
      end
    end
  end
  records.all
end

#scaffold_habtm_reflection_options(association) ⇒ Object

Return the class, left foreign key, right foreign key, and join table for this habtm association



136
137
138
139
# File 'lib/scaffolding_extensions/model/sequel.rb', line 136

def scaffold_habtm_reflection_options(association)
  reflection = scaffold_association(association)
  [reflection.associated_class, reflection[:left_key], reflection[:right_key], reflection[:join_table]]
end

#scaffold_new_associated_object_values(association, record) ⇒ Object

Returns a hash of values to be used as url parameters on the link to create a new :has_many associated object. Defaults to setting the foreign key field to the record’s primary key.



144
145
146
# File 'lib/scaffolding_extensions/model/sequel.rb', line 144

def scaffold_new_associated_object_values(association, record)
  {scaffold_foreign_key(association_reflection(association))=>record.pk}
end

#scaffold_new_object(*args) ⇒ Object

Set *_on_save_failure = false



149
150
151
152
153
154
# File 'lib/scaffolding_extensions/model/sequel.rb', line 149

def scaffold_new_object(*args)
  obj = super
  obj.raise_on_save_failure = false
  obj.raise_on_typecast_failure = false
  obj
end

#scaffold_primary_keyObject

The primary key for the given table



157
158
159
# File 'lib/scaffolding_extensions/model/sequel.rb', line 157

def scaffold_primary_key
  primary_key
end

#scaffold_save(action, object) ⇒ Object

Saves the object.



162
163
164
# File 'lib/scaffolding_extensions/model/sequel.rb', line 162

def scaffold_save(action, object)
  object.save
end

#scaffold_table_column_type(column) ⇒ Object

Get the column type from the schema. Sequel doesn’t differentiate between string and text columns (since both are the same in ruby), so check if the database type is text or if more than 255 characters allowed in the field and return :text if the type is string.



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/scaffolding_extensions/model/sequel.rb', line 170

def scaffold_table_column_type(column)
  if String === column
    return nil unless columns.map{|x| x.to_s}.include?(column)
    column = column.to_sym
  end
  if column_info = db_schema[column] and type = column_info[:type]
    if type == :string && (column_info[:db_type] == "text" || ((mc = column_info[:max_chars]) && mc > 255))
      :text
    else
      type
    end
  end
end

#scaffold_table_nameObject

The name of the underlying table



185
186
187
# File 'lib/scaffolding_extensions/model/sequel.rb', line 185

def scaffold_table_name
  table_name
end

#scaffold_use_eager_graphObject

Whether to use eager_graph instead of eager for eager loading. This is necessary if you need to reference associated tables when filtering. Can be set with an instance variable.



192
193
194
# File 'lib/scaffolding_extensions/model/sequel.rb', line 192

def scaffold_use_eager_graph
  @scaffold_use_eager_graph ||= false
end

#transaction(&block) ⇒ Object

Sequel doesn’t allow you to use transaction on a model (only on a database), so add a transaction method that starts a transaction on the associated database.



198
199
200
# File 'lib/scaffolding_extensions/model/sequel.rb', line 198

def transaction(&block)
  db.transaction(&block)
end