Class: ActionController::Base
Class Method Summary collapse
-
.somekool_scaffold(model, options = {}) ⇒ Object
def self.metaclass; class << self; self; end; end.
Class Method Details
.somekool_scaffold(model, options = {}) ⇒ Object
def self.metaclass; class << self; self; end; end
60 61 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 88 89 90 91 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 130 131 132 133 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 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 |
# File 'lib/smklib/somekool_scaffold.rb', line 60 def self.somekool_scaffold(model, = {}) #raise Object.const_get(model.to_s.singularize.capitalize).inspect instance_eval do # required for in_place_editor_field include ActionView::Helpers::TagHelper include ActionView::Helpers::JavaScriptHelper include ActionView::Helpers::JavaScriptMacrosHelper # get model object model_sym = model.to_s.singularize object_model = Object.const_get(model_sym.capitalize) model_sym = model_sym.downcase.to_sym # call in_plate_edit_for all columns columns = object_model.content_columns.collect do |c| if [:ignore_columns].include? c.name.to_sym nil else in_place_edit_for model_sym, c.name.to_sym c end end.compact [:foreign_keys].each do |fk| in_place_edit_for model_sym, fk[:key].to_sym define_method("set_#{model_sym}_#{fk[:key]}".to_sym) do a = object_model.find(params[:id]) a[fk[:key].to_sym] = params[:value] a.save render :text => (a[fk[:key].to_sym].nil? ? _('NULL') : "#{a[fk[:key].to_sym]} - #{fk[:model].find(a[fk[:key].to_sym]).to_s}".nbsp) end end # simple action index define_method(:index) do redirect_to :action => 'list' end # main action define_method(:list) do xml = Builder::XmlMarkup.new(:indent=>2) xml.a(:class => "#{model_sym}_add_link", :href => "/#{model_sym.to_s.pluralize}/add") do xml.img(:title => 'add_button', :alt => 'add_button', :border => 0, :src => '/smklib/images/add.png') xml << _('Add new %s') % model_sym.to_s end xml.table(:cellpadding => 0, :cellspacing => 0, :border => 0, :class => "somekool_scaffold list #{model} #{[:css_class]}") do xml.tr do xml.th _('ID') unless [:no_id] columns.each do |c| xml.th { xml << c.human_name.to_s.nbsp} end [:foreign_keys].each do |fk| fk[:collection] = ([[nil, _('NULL')]] + fk[:model].find_all.collect { |item| [item.id, item.to_s] }).inspect.gsub(/nil/, 'null') xml.th fk[:title] end if [:foreign_keys] xml.th { xml << _('Delete') } unless [:no_delete] end object_model.find_all.each_with_index do |row, row_index| instance_variable_set("@#{model_sym}", row) xml.tr(:class => row_index % 2 == 0 ? 'even' : 'odd') do xml.td do xml << instance_variable_get("@#{model_sym}")[:id].to_s end unless [:no_id] columns.each do |c| xml.td do #xml << row.send(c.name) instance_variable_get("@#{model_sym}")[c.name.to_sym] = " " if instance_variable_get("@#{model_sym}")[c.name.to_sym].to_s.empty? xml << in_place_editor_field(model_sym, c.name) end end [:foreign_keys].each do |fk| begin front_val = "#{row[fk[:key]]} - #{fk[:model].find(row[fk[:key]]).to_s}".nbsp rescue front_val = _('NULL') end xml.td do xml << in_place_editor_dropdown(model_sym, fk[:key], {:front_val => front_val}, {:value => row[fk[:key]], :collection => fk[:collection]}) end #xml.td fk[:model].find(:first, :conditions => ["#{fk[:key]} = ?", row[fk[:key]]]).inspect ## same end if [:foreign_keys] xml.td do xml.a(:class => "#{model_sym}_delete_link", :href => "/#{model_sym.to_s.pluralize}/destroy/#{row.id}") do xml.img(:title => 'delete_button', :alt => 'delete_button', :border => 0, :src => '/smklib/images/trashcan_empty.png') end end unless [:no_delete] end end end render :text => xml.target!, :layout => 'main' end define_method(:add) do xml = Builder::XmlMarkup.new(:indent=>2) xml.form(:action => "/#{model_sym.to_s.pluralize}/create") do xml.table(:cellpadding => 0, :cellspacing => 0, :border => 0, :class => "somekool_scaffold add #{model} #{[:css_class]}") do columns.each do |c| xml.tr do xml.th { xml << c.human_name.to_s.nbsp} xml.td { xml.input(:type => 'text', :name => "#{model_sym}[#{c.name}]") } end end [:foreign_keys].each do |fk| fk[:collection] = ([[nil, _('NULL')]] + fk[:model].find_all.collect { |item| [item.id, item.to_s] }) xml.tr do xml.th fk[:title] xml.td do xml.select(:name => "#{model_sym}[#{fk[:key]}]") do fk[:collection].each do |opt| xml.option(:value => opt[0]) { xml << opt[1] } end end end end end if [:foreign_keys] xml.tr do xml.td(:colspan => 2) { xml.input(:type => 'submit', :value => _('Add'))} end end end render :text => xml.target!, :layout => 'main' end define_method(:create) do object_model.create(params[model_sym]) flash[:notice] = _('New %s created') % model_sym.to_s redirect_to_path "/#{model_sym.to_s.pluralize}/list" end #define_method(:edit) do #end #define_method(:update) do #end define_method(:destroy) do object_model.find(params[:id]).destroy flash[:notice] = _('%s %s has been destroyed') % [model_sym.to_s, params[:id].to_s] redirect_to_path "/#{model_sym.to_s.pluralize}/list" end end end |