Module ScaffoldingExtensions::MetaSequel

  1. lib/scaffolding_extensions/model/sequel.rb

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

Constants

SCAFFOLD_OPTIONS = ::ScaffoldingExtensions::MetaModel::SCAFFOLD_OPTIONS

Public instance methods

scaffold_add_associated_object (association, object, associated_object)

Add the associated object to the object’s association

[show source]
    # File lib/scaffolding_extensions/model/sequel.rb, line 21
21:   def scaffold_add_associated_object(association, object, associated_object)
22:     object.send(association_reflection(association).add_method, associated_object) unless object.send(association).include?(associated_object)
23:   end
scaffold_all_associations ()

Array of all association reflections for this model

[show source]
    # File lib/scaffolding_extensions/model/sequel.rb, line 26
26:   def scaffold_all_associations
27:     all_association_reflections
28:   end
scaffold_associated_class (association)

The class that this model is associated with via the association

[show source]
    # File lib/scaffolding_extensions/model/sequel.rb, line 31
31:   def scaffold_associated_class(association)
32:     association_reflection(association).associated_class
33:   end
scaffold_associated_objects (association, object, options)

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.

[show source]
    # File lib/scaffolding_extensions/model/sequel.rb, line 39
39:   def scaffold_associated_objects(association, object, options)
40:     object.send(association)
41:   end
scaffold_association (association)

The association reflection for this association

[show source]
    # File lib/scaffolding_extensions/model/sequel.rb, line 44
44:   def scaffold_association(association)
45:     association_reflection(association)
46:   end
scaffold_association_type (association)

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.

[show source]
    # File lib/scaffolding_extensions/model/sequel.rb, line 51
51:   def scaffold_association_type(association)
52:     case (a = scaffold_association(association))[:type]
53:       when :one_to_many
54:         a[:one_to_one] ? :one : :new
55:       when :many_to_many
56:         :edit
57:       else
58:         :one
59:     end
60:   end
scaffold_associations ()

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

[show source]
    # File lib/scaffolding_extensions/model/sequel.rb, line 64
64:   def scaffold_associations
65:     @scaffold_associations ||= associations.sort_by{|name| name.to_s}
66:   end
scaffold_destroy (object)

Destroys the object

[show source]
    # File lib/scaffolding_extensions/model/sequel.rb, line 69
69:   def scaffold_destroy(object)
70:     object.destroy
71:   end
scaffold_error_raised ()

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

[show source]
    # File lib/scaffolding_extensions/model/sequel.rb, line 74
74:   def scaffold_error_raised
75:     Sequel::Error
76:   end
scaffold_fields (action = :default)

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.

[show source]
    # File lib/scaffolding_extensions/model/sequel.rb, line 82
82:   def scaffold_fields(action = :default)
83:     return @scaffold_fields if @scaffold_fields
84:     fields = columns - [primary_key]
85:     all_association_reflections.each do |reflection|
86:       next unless reflection[:type] == :many_to_one
87:       fields.delete(reflection[:key].to_sym)
88:       fields.push(reflection[:name])
89:     end
90:     @scaffold_fields = fields.sort_by{|f| f.to_s}
91:   end
scaffold_find_object (*args)

Set *_on_save_failure = false

[show source]
    # File lib/scaffolding_extensions/model/sequel.rb, line 94
94:   def scaffold_find_object(*args)
95:     obj = super
96:     obj.raise_on_save_failure = false
97:     obj.raise_on_typecast_failure = false
98:     obj
99:   end
scaffold_foreign_key (reflection)

The foreign key for the given reflection

[show source]
     # File lib/scaffolding_extensions/model/sequel.rb, line 102
102:   def scaffold_foreign_key(reflection)
103:     reflection[:key]
104:   end
scaffold_get_object (id)

Retrieve a single model object given an id

[show source]
     # File lib/scaffolding_extensions/model/sequel.rb, line 107
107:   def scaffold_get_object(id)
108:     self[id.to_i] || (raise scaffold_error_raised)
109:   end
scaffold_get_objects (options)

Retrieve multiple objects given a hash of options

[show source]
     # File lib/scaffolding_extensions/model/sequel.rb, line 112
112:   def scaffold_get_objects(options)
113:     records = dataset
114:     records = records.send(scaffold_use_eager_graph ? :eager_graph : :eager, *options[:include]) if options[:include]
115:     records = records.order(*options[:order]) if options[:order]
116:     records = records.limit(options[:limit], options[:offset]) if options[:limit]
117:     conditions = options[:conditions]
118:     if conditions && Array === conditions && conditions.length > 0
119:       if String === conditions[0]
120:         records = records.filter(*conditions)
121:       else
122:         conditions.each do |cond|
123:           next if cond.nil?
124:           records = case cond
125:             when Hash, String then records.filter(cond)
126:             when Array then records.filter(*cond)
127:             when Proc then records.filter(&cond)
128:           end
129:         end
130:       end
131:     end
132:     records.all
133:   end
scaffold_habtm_reflection_options (association)

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

[show source]
     # File lib/scaffolding_extensions/model/sequel.rb, line 136
136:   def scaffold_habtm_reflection_options(association)
137:     reflection = scaffold_association(association)
138:     [reflection.associated_class, reflection[:left_key], reflection[:right_key], reflection[:join_table]]
139:   end
scaffold_new_associated_object_values (association, record)

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.

[show source]
     # File lib/scaffolding_extensions/model/sequel.rb, line 144
144:   def scaffold_new_associated_object_values(association, record)
145:     {scaffold_foreign_key(association_reflection(association))=>record.pk}
146:   end
scaffold_new_object (*args)

Set *_on_save_failure = false

[show source]
     # File lib/scaffolding_extensions/model/sequel.rb, line 149
149:   def scaffold_new_object(*args)
150:     obj = super
151:     obj.raise_on_save_failure = false
152:     obj.raise_on_typecast_failure = false
153:     obj
154:   end
scaffold_primary_key ()

The primary key for the given table

[show source]
     # File lib/scaffolding_extensions/model/sequel.rb, line 157
157:   def scaffold_primary_key
158:     primary_key
159:   end
scaffold_save (action, object)

Saves the object.

[show source]
     # File lib/scaffolding_extensions/model/sequel.rb, line 162
162:   def scaffold_save(action, object)
163:     object.save
164:   end
scaffold_table_column_type (column)

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.

[show source]
     # File lib/scaffolding_extensions/model/sequel.rb, line 170
170:   def scaffold_table_column_type(column)
171:     if String === column
172:       return nil unless columns.map{|x| x.to_s}.include?(column)
173:       column = column.to_sym
174:     end
175:     if column_info = db_schema[column] and type = column_info[:type]
176:       if type == :string && (column_info[:db_type] == "text" || ((mc = column_info[:max_chars]) && mc > 255))
177:         :text
178:       else
179:         type
180:       end
181:     end
182:   end
scaffold_table_name ()

The name of the underlying table

[show source]
     # File lib/scaffolding_extensions/model/sequel.rb, line 185
185:   def scaffold_table_name
186:     table_name
187:   end
scaffold_use_eager_graph ()

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.

[show source]
     # File lib/scaffolding_extensions/model/sequel.rb, line 192
192:   def scaffold_use_eager_graph
193:     @scaffold_use_eager_graph ||= false
194:   end
transaction (&block)

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.

[show source]
     # File lib/scaffolding_extensions/model/sequel.rb, line 198
198:   def transaction(&block)
199:     db.transaction(&block)
200:   end