Module ScaffoldingExtensions::MetaDataMapper

  1. lib/scaffolding_extensions/model/datamapper.rb

Class methods added to DataMapper::Resource 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/datamapper.rb, line 65
65:   def scaffold_add_associated_object(association, object, associated_object)
66:     ap = object.send(association)
67:     ap << associated_object unless ap.include?(associated_object)
68:     object.save
69:   end
scaffold_all_associations ()

Array of all association reflections for this model only shows the associations that are scaffolding_enabled

[show source]
    # File lib/scaffolding_extensions/model/datamapper.rb, line 73
73:   def scaffold_all_associations
74:     relationships.values.select { |v|
75:       v.send(:target_model).respond_to?(:scaffold_name)
76:     }
77:   end
scaffold_associated_class (association)

The class that this model is associated with via the association

[show source]
    # File lib/scaffolding_extensions/model/datamapper.rb, line 80
80:   def scaffold_associated_class(association)
81:     relationships[association].target_model
82:   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/datamapper.rb, line 149
149:   def scaffold_associated_objects(association, object, options)
150:     object.send(association,:order => get_ordering_options(scaffold_select_order_association(association)))
151:   end
scaffold_association (association)

The association reflection for this association

[show source]
    # File lib/scaffolding_extensions/model/datamapper.rb, line 85
85:   def scaffold_association(association)
86:     relationships[association]
87:   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/datamapper.rb, line 92
 92:   def scaffold_association_type(association)
 93:     if relationships[association].class == DataMapper::Associations::OneToMany::Relationship
 94:         :new
 95:     elsif relationships[association].class == DataMapper::Associations::ManyToMany::Relationship
 96:         :edit
 97:     else
 98:         :one
 99:     end
100:   end
scaffold_associations ()

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

[show source]
     # File lib/scaffolding_extensions/model/datamapper.rb, line 104
104:   def scaffold_associations
105:     @scaffold_associations ||= relationships.keys.select { |v|
106:       relationships[v].send(:target_model).respond_to?(:scaffold_name)
107:     }.sort_by{|name| name.to_s}
108:   end
scaffold_destroy (object)

Destroys the object

[show source]
     # File lib/scaffolding_extensions/model/datamapper.rb, line 111
111:   def scaffold_destroy(object)
112:     object.destroy
113:   end
scaffold_error_raised ()

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

[show source]
     # File lib/scaffolding_extensions/model/datamapper.rb, line 116
116:   def scaffold_error_raised
117:     DataMapper::ObjectNotFoundError
118:   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/datamapper.rb, line 124
124:   def scaffold_fields(action = :default)
125:     return @scaffold_fields if @scaffold_fields
126:     fields = (properties.map {|a| a.name}) - [scaffold_primary_key]
127:     scaffold_all_associations.each do |reflection|
128:       next unless reflection.class == DataMapper::Associations::ManyToOne::Relationship
129:       fields.delete(get_key_array_safe(reflection.send(:child_key)).name)
130:       fields.push(reflection.name)
131:     end
132:     @scaffold_fields = fields.sort_by{|f| f.to_s}
133:   end
scaffold_foreign_key (reflection)

The foreign key for the given reflection

[show source]
     # File lib/scaffolding_extensions/model/datamapper.rb, line 136
136:   def scaffold_foreign_key(reflection)
137:     get_key_array_safe(reflection.child_key).name
138:   end
scaffold_get_object (id)

Retrieve a single model object given an id

[show source]
     # File lib/scaffolding_extensions/model/datamapper.rb, line 141
141:   def scaffold_get_object(id)
142:     self.get(id) || (raise scaffold_error_raised)
143:   end
scaffold_get_objects (options)

Retrieve multiple objects given a hash of options

[show source]
     # File lib/scaffolding_extensions/model/datamapper.rb, line 154
154:   def scaffold_get_objects(options)
155:     optionshash = {}
156:     data = self.all
157:     if options[:conditions]
158:       conditions = options[:conditions]
159:       if conditions && Array === conditions && conditions.length > 0
160:         if String === conditions[0]
161:           data = data.all(:conditions => conditions)
162:         else
163:           conditions.each do |cond|
164:             next if cond.nil?
165:             data = case cond
166:               when Hash, String then data.all(:conditions => [cond.gsub("NULL","?"),nil])
167:               when Array then 
168:                 if cond.length==1
169:                   data.all(:conditions => [cond[0].gsub("NULL","?"),nil])
170:                 else
171:                   data.all(:conditions => cond)
172:                 end
173:               when Proc then data.all(&cond)
174:             end
175:           end
176:         end
177:       end
178:     end
179:     slice = nil
180:     if options[:limit]
181:       startpos = options[:offset] || 0
182:       endpos = options[:limit]
183:       slice = [startpos,endpos]
184:     end
185:     # TODO includes break SQL generation
186:     # optionshash[:links] = options[:include] if options[:include]
187:     # optionshash[:links] = [optionshash[:links]] unless optionshash[:links].is_a?(Array)
188:     if options[:order] then
189:       optionshash[:order] = get_ordering_options(options[:order])
190:     end
191:     if slice then
192:       q = data.all(optionshash).slice(*slice)
193:     else
194:       q = data.all(optionshash)
195:     end
196:     #p repository.adapter.send("select_statement",q.query)
197:     q.to_a
198:   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/datamapper.rb, line 201
201:   def scaffold_habtm_reflection_options(association)
202:     habtm = relationships[association]
203:     [
204:       habtm.target_model,
205:       get_key_array_safe(habtm.through.child_key).name,
206:       get_key_array_safe(habtm.via.child_key).name,
207:       habtm.send(:through_model).storage_name
208:     ]
209:   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/datamapper.rb, line 214
214:   def scaffold_new_associated_object_values(association, record)
215:     {scaffold_foreign_key(scaffold_association(association))=>record.scaffold_id}
216:   end
scaffold_primary_key ()

The primary key for the given table

[show source]
     # File lib/scaffolding_extensions/model/datamapper.rb, line 219
219:   def scaffold_primary_key
220:     get_key_array_safe(key).name
221:   end
scaffold_save (action, object)

Saves the object.

[show source]
     # File lib/scaffolding_extensions/model/datamapper.rb, line 224
224:   def scaffold_save(action, object)
225:     object.save
226:   end
scaffold_table_column_type (c)

The column type for the given table column, or nil if it isn’t a table column

[show source]
     # File lib/scaffolding_extensions/model/datamapper.rb, line 229
229:   def scaffold_table_column_type(c)
230:     column = self.properties[c]
231:     if column then
232:       if column.type == DataMapper::Property::Text
233:         :text
234:       else
235:         column.class.to_s.split("::").last.downcase.intern
236:       end
237:     else
238:       nil
239:     end
240:   end
scaffold_table_name ()

The name of the underlying table

[show source]
     # File lib/scaffolding_extensions/model/datamapper.rb, line 243
243:   def scaffold_table_name
244:     storage_name
245:   end