Class: UzuUzu::Controller::Crud
- Inherits:
-
Object
- Object
- UzuUzu::Controller::Crud
- Defined in:
- lib/uzuuzu-cms/controller/crud.rb
Constant Summary collapse
- DEFAULT_LIMIT =
20
Instance Method Summary collapse
- #destroy(model_name, id, confirm = nil) ⇒ Object
- #edit(model_name, id) ⇒ Object
- #index(model_name) ⇒ Object
- #is_model?(model) ⇒ Boolean
- #model_create(model_name) ⇒ Object
- #new(model_name) ⇒ Object
- #show(model_name, id) ⇒ Object
Instance Method Details
#destroy(model_name, id, confirm = nil) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/uzuuzu-cms/controller/crud.rb', line 176 def destroy(model_name, id, confirm=nil) @model_name = model_name model = model_create(model_name) if model.nil? return "faild model_name = #{model_name}" end UzuUzu.transaction do |tr| @resource = model.first(:id => id) if confirm.nil? @resource.destroy end end respond_to do |wish| case(wish) when :xhr respond(render_file(__DIR__("view/crud/destroy.rhtml"))) when :html redirect r(@model_name) if confirm.nil? respond(render_file(__DIR__("view/crud/destroy.rhtml"))) when :xml @resource.to_xml when :yaml, :yml @resource.to_yaml when :json, :js @resource.to_json when :csv @resource.to_csv else not_found end end end |
#edit(model_name, id) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/uzuuzu-cms/controller/crud.rb', line 134 def edit(model_name, id) @model_name = model_name model = model_create(model_name) if model.nil? return "faild model_name = #{model_name}" end saved = false UzuUzu.transaction do |tr| @resource = model.first(:id => id) if request.post? request.params.each do |key, value| @resource[key] = value unless value.blank? @resource[key] = nil if value.blank? end if @resource.valid? saved = @resource.save end end end respond_to do |wish| case(wish) when :html, :htm redirect r(@model_name) if saved respond(render_file(__DIR__("view/crud/edit.rhtml"))) when :xml @resource.to_xml when :yaml, :yml @resource.to_yaml when :json, :js @resource.to_json when :csv @resource.to_csv else not_found end end end |
#index(model_name) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/uzuuzu-cms/controller/crud.rb', line 15 def index(model_name) @model_name = model_name UzuUzu.logger.debug UzuUzu.datastore model = model_create(model_name) if model.nil? return "faild model_name = #{model_name}" end @limit = request.params[:limit] || DEFAULT_LIMIT @page = request.params[:page] @query = request.params[:query] condition = {:limit => @limit} condition[:offset] = @page * @limit unless @page.nil? #create query for serch if @query querys = @query.split(/[¥s ]/) querys.each do |query_set| query_set = query_set.split(':') condition[query_set[0].to_sym] = query_set[1] if query_set.size >= 2 end end UzuUzu.repository do @list = model.all(condition) end respond_to do |wish| case(wish) when :html, :htm respond(render_file(__DIR__("view/crud/index.rhtml"))) when :xml @list.to_xml when :yaml, :yml @list.to_yaml when :json, :js @list.to_json when :csv @list.to_csv else not_found end end end |
#is_model?(model) ⇒ Boolean
240 241 242 |
# File 'lib/uzuuzu-cms/controller/crud.rb', line 240 def is_model?(model) model.kind_of?(DataMapper::Model) end |
#model_create(model_name) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/uzuuzu-cms/controller/crud.rb', line 213 def model_create(model_name) return unless /^[_0-9a-zA-Z]+$/ =~ model_name model_name = model_name.split('__').map do |name| name.camel_case end.join('::') begin model = eval("::#{model_name}") rescue NameError => e if UzuUzu.const_defined?(model_name) model = UzuUzu.const_get(model_name) else UzuUzu.logger.debug "model create missing : #{model_name}" end end unless model.nil? if is_model?(model) return model else UzuUzu.logger.debug "model type missing : #{model_name}" end end nil end |
#new(model_name) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/uzuuzu-cms/controller/crud.rb', line 92 def new(model_name) @model_name = model_name model = model_create(model_name) if model.nil? return "faild model_name = #{model_name}" end saved = false UzuUzu.transaction do |tr| @resource = model.new if request.post? request.params.each do |key, value| @resource[key] = value unless value.blank? @resource[key] = nil if value.blank? end if @resource.valid? saved = @resource.save end end end respond_to do |wish| case(wish) when :html, :htm redirect r(@model_name) if saved respond(render_file(__DIR__("view/crud/new.rhtml"))) when :xml @resource.to_xml when :yaml, :yml @resource.to_yaml when :json, :js @resource.to_json when :csv @resource.to_csv else not_found end end end |
#show(model_name, id) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/uzuuzu-cms/controller/crud.rb', line 62 def show(model_name, id) @model_name = model_name model = model_create(model_name) if model.nil? return "faild model_name = #{model_name}" end UzuUzu.repository do @resource = model.first(:id => id) end respond_to do |wish| case(wish) when :html, :htm respond(render_file(__DIR__("view/crud/show.rhtml"))) when :xml @resource.to_xml when :yaml, :yml @resource.to_yaml when :json, :js @resource.to_json when :csv @resource.to_csv else not_found end end end |