Object
Defines the generic ORM management functions used to manipulate data for admin. @private
# File lib/padrino-admin/generators/orm.rb, line 16 def initialize(name, orm, columns=nil, column_fields=nil) name = name.to_s @klass_name = name.camelize @klass = @klass_name.constantize rescue nil @name_singular = name.underscore.gsub(/^.*\//, '') # convert submodules i.e. FooBar::Jank.all # => jank @name_plural = @name_singular.pluralize @orm = orm.to_sym @columns = columns @column_fields = column_fields raise OrmError, "Model '#{klass_name}' could not be found!" if @columns.nil? && @klass.nil? end
# File lib/padrino-admin/generators/orm.rb, line 82 def all "#{klass_name}.all" end
# File lib/padrino-admin/generators/orm.rb, line 95 def build(params=nil) if params "#{klass_name}.new(#{params})" else "#{klass_name}.new" end end
# File lib/padrino-admin/generators/orm.rb, line 73 def column_fields excluded_columns = ]id created_at updated_at] column_fields = columns.dup column_fields.reject! { |column| excluded_columns.include?(column.name.to_s) } @column_fields ||= column_fields.map do |column| { :name => column.name, :field_type => field_type(column.type) } end end
# File lib/padrino-admin/generators/orm.rb, line 42 def columns @columns ||= case orm when :activerecord then @klass.columns when :datamapper then @klass.properties.map { |p| dm_column(p) } when :couchrest then @klass.properties when :mongoid then @klass.fields.values when :mongomapper then @klass.keys.values.reject { |key| key.name == "_id" } # On MongoMapper keys are an hash when :sequel then @klass.db_schema.map { |k,v| v[:type] = :text if v[:db_type] =~ /^text/; Column.new(k, v[:type]) } else raise OrmError, "Adapter #{orm} is not yet supported!" end end
# File lib/padrino-admin/generators/orm.rb, line 119 def destroy "#{name_singular}.destroy" end
# File lib/padrino-admin/generators/orm.rb, line 54 def dm_column(p) case p when DataMapper::Property::Text Column.new(p.name, :text) when DataMapper::Property::Boolean Column.new(p.name, :boolean) when DataMapper::Property::Integer Column.new(p.name, :integer) when DataMapper::Property::Decimal Column.new(p.name, :decimal) when DataMapper::Property::Float Column.new(p.name, :float) when DataMapper::Property::String Column.new(p.name, :string) else #if all fails, lets assume its stringish Column.new(p.name, :string) end end
# File lib/padrino-admin/generators/orm.rb, line 28 def field_type(type) type = :string if type.nil? # couchrest-Hack to avoid the next line to fail type = type.to_s.demodulize.downcase.to_sym unless type.is_a?(Symbol) case type when :integer, :float, :decimal then :text_field when :string then :text_field when :text then :text_area when :boolean then :check_box else :text_field end end
# File lib/padrino-admin/generators/orm.rb, line 86 def find(params=nil) case orm when :activerecord, :mongomapper, :mongoid then "#{klass_name}.find(#{params})" when :datamapper, :couchrest then "#{klass_name}.get(#{params})" when :sequel then "#{klass_name}[#{params}]" else raise OrmError, "Adapter #{orm} is not yet supported!" end end
# File lib/padrino-admin/generators/orm.rb, line 103 def save case orm when :sequel then "(@#{name_singular}.save rescue false)" else "@#{name_singular}.save" end end
# File lib/padrino-admin/generators/orm.rb, line 110 def update_attributes(params=nil) case orm when :activerecord, :mongomapper, :mongoid, :couchrest then "@#{name_singular}.update_attributes(#{params})" when :datamapper then "@#{name_singular}.update(#{params})" when :sequel then "@#{name_singular}.modified! && @#{name_singular}.update(#{params})" else raise OrmError, "Adapter #{orm} is not yet supported!" end end
Generated with the Darkfish Rdoc Generator 2.