Module: ScaffoldingExtensions::MetaDataMapper
- Defined in:
- lib/scaffolding_extensions/model/datamapper.rb
Overview
Class methods added to DataMapper::Resource to allow it to work with Scaffolding Extensions.
Constant Summary collapse
- SCAFFOLD_OPTIONS =
::ScaffoldingExtensions::MetaModel::SCAFFOLD_OPTIONS
Instance Method Summary collapse
-
#scaffold_add_associated_object(association, object, associated_object) ⇒ Object
Add the associated object to the object’s association.
-
#scaffold_all_associations ⇒ Object
Array of all association reflections for this model only shows the associations that are scaffolding_enabled.
-
#scaffold_associated_class(association) ⇒ Object
The class that this model is associated with via the association.
-
#scaffold_associated_objects(association, object, options) ⇒ Object
All objects that are currently associated with the given object.
-
#scaffold_association(association) ⇒ Object
The association reflection for this association.
-
#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.
-
#scaffold_associations ⇒ Object
List of symbols for associations to display on the scaffolded edit page.
-
#scaffold_destroy(object) ⇒ Object
Destroys the object.
-
#scaffold_error_raised ⇒ Object
The error to raise, should match other errors raised by the underlying library.
-
#scaffold_fields(action = :default) ⇒ Object
Returns the list of fields to display on the scaffolded forms.
-
#scaffold_foreign_key(reflection) ⇒ Object
The foreign key for the given reflection.
-
#scaffold_get_object(id) ⇒ Object
Retrieve a single model object given an id.
-
#scaffold_get_objects(options) ⇒ Object
Retrieve multiple objects given a hash of options.
-
#scaffold_habtm_reflection_options(association) ⇒ Object
Return the class, left foreign key, right foreign key, and join table for this habtm association.
-
#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.
-
#scaffold_primary_key ⇒ Object
The primary key for the given table.
-
#scaffold_save(action, object) ⇒ Object
Saves the object.
-
#scaffold_table_column_type(c) ⇒ Object
The column type for the given table column, or nil if it isn’t a table column.
-
#scaffold_table_name ⇒ Object
The name of the underlying table.
Instance Method Details
#scaffold_add_associated_object(association, object, associated_object) ⇒ Object
Add the associated object to the object’s association
65 66 67 68 69 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 65 def scaffold_add_associated_object(association, object, associated_object) ap = object.send(association) ap << associated_object unless ap.include?(associated_object) object.save end |
#scaffold_all_associations ⇒ Object
Array of all association reflections for this model only shows the associations that are scaffolding_enabled
73 74 75 76 77 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 73 def scaffold_all_associations relationships.values.select { |v| v.send(:target_model).respond_to?(:scaffold_name) } end |
#scaffold_associated_class(association) ⇒ Object
The class that this model is associated with via the association
80 81 82 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 80 def scaffold_associated_class(association) relationships[association].target_model 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.
149 150 151 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 149 def scaffold_associated_objects(association, object, ) object.send(association,:order => (scaffold_select_order_association(association))) end |
#scaffold_association(association) ⇒ Object
The association reflection for this association
85 86 87 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 85 def scaffold_association(association) relationships[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.
92 93 94 95 96 97 98 99 100 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 92 def scaffold_association_type(association) if relationships[association].class == DataMapper::Associations::OneToMany::Relationship :new elsif relationships[association].class == DataMapper::Associations::ManyToMany::Relationship :edit else :one end end |
#scaffold_associations ⇒ Object
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.
104 105 106 107 108 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 104 def scaffold_associations @scaffold_associations ||= relationships.keys.select { |v| relationships[v].send(:target_model).respond_to?(:scaffold_name) }.sort_by{|name| name.to_s} end |
#scaffold_destroy(object) ⇒ Object
Destroys the object
111 112 113 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 111 def scaffold_destroy(object) object.destroy end |
#scaffold_error_raised ⇒ Object
The error to raise, should match other errors raised by the underlying library.
116 117 118 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 116 def scaffold_error_raised DataMapper::ObjectNotFoundError 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.
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 124 def scaffold_fields(action = :default) return @scaffold_fields if @scaffold_fields fields = (properties.map {|a| a.name}) - [scaffold_primary_key] scaffold_all_associations.each do |reflection| next unless reflection.class == DataMapper::Associations::ManyToOne::Relationship fields.delete(get_key_array_safe(reflection.send(:child_key)).name) fields.push(reflection.name) end @scaffold_fields = fields.sort_by{|f| f.to_s} end |
#scaffold_foreign_key(reflection) ⇒ Object
The foreign key for the given reflection
136 137 138 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 136 def scaffold_foreign_key(reflection) get_key_array_safe(reflection.child_key).name end |
#scaffold_get_object(id) ⇒ Object
Retrieve a single model object given an id
141 142 143 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 141 def scaffold_get_object(id) self.get(id) || (raise scaffold_error_raised) end |
#scaffold_get_objects(options) ⇒ Object
Retrieve multiple objects given a hash of options
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 154 def scaffold_get_objects() = {} data = self.all if [:conditions] conditions = [:conditions] if conditions && Array === conditions && conditions.length > 0 if String === conditions[0] data = data.all(:conditions => conditions) else conditions.each do |cond| next if cond.nil? data = case cond when Hash, String then data.all(:conditions => [cond.gsub("NULL","?"),nil]) when Array then if cond.length==1 data.all(:conditions => [cond[0].gsub("NULL","?"),nil]) else data.all(:conditions => cond) end when Proc then data.all(&cond) end end end end end slice = nil if [:limit] startpos = [:offset] || 0 endpos = [:limit] slice = [startpos,endpos] end # TODO includes break SQL generation # optionshash[:links] = options[:include] if options[:include] # optionshash[:links] = [optionshash[:links]] unless optionshash[:links].is_a?(Array) if [:order] then [:order] = ([:order]) end if slice then q = data.all().slice(*slice) else q = data.all() end #p repository.adapter.send("select_statement",q.query) q.to_a end |
#scaffold_habtm_reflection_options(association) ⇒ Object
Return the class, left foreign key, right foreign key, and join table for this habtm association
201 202 203 204 205 206 207 208 209 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 201 def (association) habtm = relationships[association] [ habtm.target_model, get_key_array_safe(habtm.through.child_key).name, get_key_array_safe(habtm.via.child_key).name, habtm.send(:through_model).storage_name ] 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.
214 215 216 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 214 def scaffold_new_associated_object_values(association, record) {scaffold_foreign_key(scaffold_association(association))=>record.scaffold_id} end |
#scaffold_primary_key ⇒ Object
The primary key for the given table
219 220 221 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 219 def scaffold_primary_key get_key_array_safe(key).name end |
#scaffold_save(action, object) ⇒ Object
Saves the object.
224 225 226 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 224 def scaffold_save(action, object) object.save end |
#scaffold_table_column_type(c) ⇒ Object
The column type for the given table column, or nil if it isn’t a table column
229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 229 def scaffold_table_column_type(c) column = self.properties[c] if column then if column.type == DataMapper::Property::Text :text else column.class.to_s.split("::").last.downcase.intern end else nil end end |
#scaffold_table_name ⇒ Object
The name of the underlying table
243 244 245 |
# File 'lib/scaffolding_extensions/model/datamapper.rb', line 243 def scaffold_table_name storage_name end |