Class: Padrino::Admin::Generators::Orm

Inherits:
Object
  • Object
show all
Defined in:
padrino-admin/lib/padrino-admin/generators/orm.rb

Overview

Defines the generic ORM management functions used to manipulate data for admin.

Defined Under Namespace

Classes: Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, orm, columns = nil, column_fields = nil) ⇒ Orm

Returns a new instance of Orm.

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 15

def initialize(name, orm, columns=nil, column_fields=nil)
  name            = name.to_s
  @klass_name     = name.underscore.camelize
  @klass          = @klass_name.constantize rescue nil
  @name_param     = name.underscore.gsub(/\//, '_')
  @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!\nPerhaps you would like to run 'bundle exec padrino g model #{klass_name}' to create it first?" if @columns.nil? && @klass.nil?
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



13
14
15
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 13

def klass
  @klass
end

#klass_nameObject (readonly)

Returns the value of attribute klass_name.



13
14
15
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 13

def klass_name
  @klass_name
end

#name_paramObject (readonly)

Returns the value of attribute name_param.



13
14
15
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 13

def name_param
  @name_param
end

#name_pluralObject (readonly)

Returns the value of attribute name_plural.



13
14
15
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 13

def name_plural
  @name_plural
end

#name_singularObject (readonly)

Returns the value of attribute name_singular.



13
14
15
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 13

def name_singular
  @name_singular
end

#ormObject (readonly)

Returns the value of attribute orm.



13
14
15
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 13

def orm
  @orm
end

Instance Method Details

#activerecord?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 28

def activerecord?
  case orm
  when :activerecord, :minirecord then true
  else false
  end
end

#allObject



97
98
99
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 97

def all
  "#{klass_name}.all"
end

#build(params = nil) ⇒ Object



110
111
112
113
114
115
116
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 110

def build(params=nil)
  if params
    "#{klass_name}.new(#{params})"
  else
    "#{klass_name}.new"
  end
end

#column_fieldsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 83

def column_fields
  excluded_columns = %w[created_at updated_at]
  case orm
    when :mongoid then excluded_columns << '_id'
    else excluded_columns << 'id'
  end

  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

#columnsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 49

def columns
  @columns ||= case orm
    when :activerecord then @klass.columns
    when :minirecord   then @klass.columns
    when :datamapper   then @klass.properties.map { |p| dm_column(p) }
    when :couchrest    then @klass.properties
    when :mongoid      then @klass.fields.values.reject { |col| %w[_id _type].include?(col.name) }
    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/i; Column.new(k, v[:type]) }
    when :ohm          then @klass.attributes.map { |a| Column.new(a.to_s, :string) } # ohm has strings
    when :dynamoid     then @klass.attributes.map { |k,v| Column.new(k.to_s, v[:type]) }
    else raise OrmError, "Adapter #{orm} is not yet supported!"
  end
end

#destroyObject



134
135
136
137
138
139
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 134

def destroy
  case orm
    when :ohm then "#{name_singular}.delete"
    else "#{name_singular}.destroy"
  end
end

#dm_column(p) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 64

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

#field_type(type) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 35

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

#find(params = nil) ⇒ Object



101
102
103
104
105
106
107
108
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 101

def find(params=nil)
  case orm
    when :activerecord, :minirecord, :mongomapper, :mongoid, :dynamoid then "#{klass_name}.find(#{params})"
    when :datamapper, :couchrest then "#{klass_name}.get(#{params})"
    when :sequel, :ohm then "#{klass_name}[#{params}]"
    else raise OrmError, "Adapter #{orm} is not yet supported!"
  end
end

#find_by_ids(params = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 141

def find_by_ids(params=nil)
  case orm
    when :ohm then "#{klass_name}.fetch(#{params})"
    when :datamapper then "#{klass_name}.all(:id => #{params})"
    when :sequel then "#{klass_name}.where(:id => #{params})"
    when :mongoid then "#{klass_name}.find(#{params})"
    when :couchrest then "#{klass_name}.all(:keys => #{params})"
    when :dynamoid then "#{klass_name}.find(#{params})"
    else find(params)
  end
end

#has_error(field) ⇒ Object



163
164
165
166
167
168
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 163

def has_error(field)
  case orm
    when :datamapper, :ohm, :sequel then "@#{name_singular}.errors.key?(:#{field}) && @#{name_singular}.errors[:#{field}].count > 0"
    else "@#{name_singular}.errors.include?(:#{field})"
  end
end

#multiple_destroy(params = nil) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 153

def multiple_destroy(params=nil)
  case orm
    when :ohm then "#{params}.each(&:delete)"
    when :sequel then  "#{params}.destroy"
    when :datamapper then "#{params}.destroy"
    when :couchrest, :mongoid, :mongomapper, :dynamoid then "#{params}.each(&:destroy)"
    else "#{klass_name}.destroy #{params}"
  end
end

#saveObject



118
119
120
121
122
123
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 118

def save
  case orm
    when :sequel then "(@#{name_singular}.save rescue false)"
    else "@#{name_singular}.save"
  end
end

#update_attributes(params = nil) ⇒ Object



125
126
127
128
129
130
131
132
# File 'padrino-admin/lib/padrino-admin/generators/orm.rb', line 125

def update_attributes(params=nil)
  case orm
    when :mongomapper, :mongoid, :couchrest, :dynamoid then "@#{name_singular}.update_attributes(#{params})"
    when :activerecord, :minirecord, :datamapper, :ohm 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