Published at: 9.IV.2008 14:02 CET
Something ago I
wrote about the problems which arise when using models in your Rails migrations. Meanwhile I developed a really simple solution to this problem and today I wrapped it up in to a plugin called
SafeDataMigrations.
How to use it? Install it in your Rails application:
ruby script/plugin install http://svn.remvee.net/plugins/safe_data_migrations
and apply it in your migration:
class DowncaseUserNames < ActiveRecord::Migration
models :user
def self.up
User.find(:all).each do |user|
user.update_attributes(:names, user.name.downcase)
end
end
Look at the README file for a more elaborate example.
So how does it work? It simply undefines the models your referring to and redefines an empty ActiveRecord class;
Object.send :remove_const, :User rescue nil
class User < ActiveRecord::Base; end
Now you are sure to have a
User model available in your migration without any validations which may make data manipulations impossible. The undefining of an already available model also ensures you don’t need to use
ActiveRecord::Base.reset_column_information before updating new fields, unless you use your model before altering the table of course.
Update: As coderrr points out you don’t need to clobber the global scope model class because a nested model works fine too. I wrongly assumed introducing a new model class in side (!) a migration class would only reopen my original model class and keep validations intact. To illustrate:
class User; def top; end; end
class Migration
class User; def nested; end; end
def self.go
p User.instance_methods - Object.instance_methods
end
end
Migration.go
yields ["nested"] and not ["top", "nested"] as I suspected. Apparently I was bitten too hard by a problem which arose when I used an original model class to even try the above. I’ll pull the plugin because it’s pointless.. Bad me, thanks coderrr!
Published at: 24.IX.2007 21:49 CET
Last week at RailsConf Europe I met some people who are using my ActiveForm code to make forms in their Rails sites. It’s about time to wrap it up as a plugin, so here it is.
From the
README:
This plugin provides a base class for making forms with ActiveRecord validations without having a corresponding database table. You can use ActiveForm for:
- making forms which don’t needed storage, like simple email forms
- provide extra validations on existing ActiveRecord models
- make forms for composite objects
Installation:
script/plugin install http://svn.remvee.net/plugins/active_form
I know, there already is a plugin called active_form but I don’t like it. It doesn’t provide a “real” AR object causing all kinds of things to not work (like ActiveRecordHelper#form and DateHelper#datetime_select for instance), it doesn’t include any tests and my version is a lot simpler (flog score 20 versus 74).
Why not change the name? I like it! If you can come up with something better, please leave a comment.
Gepubliceerd op: 9.IV.2007 17:27 CET
In
Ruby 1.8.6 zijn er wat kleine verhuizingen met betrekking tot de
Date class doorgevoerd, met als gevolg dat m’n hackwerk, ter vernederlandsing van datum formaten, applicaties, welke gebruik maken van de dutchify plugin, onklaar heeft gemaakt. In de nieuwste versie is dit probleem opgelost;
ruby script/plugin install --force http://svn.remvee.net/plugins/dutchify
Update probleem gevonden door Stephan gerepareerd, Time wel vertaald Date niet
Update 2 probleem gevonden door Bertus gerepareerd, date_select niet meer vertaald
Update 3 oeps, niet alle tests gedraaid.. Dank je Stephan.
Gepubliceerd op: 28.VIII.2006 18:28 CET
Een nederlandstalige website bouwen met rails is als rijden in een stoptrein. Scaffolding gebruikt; de volgende halte is “het aanpassen van de knoppen en labels”, validaties toegevoegd; de volgende halte is “het vertalen van de foutmeldingen” etc. etc. Alle snelheids verhogende features zijn doordrenkt van engelse drempels. Ik wil met de intercity!
Met de dutchify plugin doe ik een poging om alle amerikanismes aan de presentatie kant aan te pakken. Ik heb me bewust alleen op nederlands gericht omdat ik me niet alle i18n en l10n problemen op de hals wil halen, deze zijn veel te gemakkelijk te onderschatten. Ook hou ik me verre van het vertalen van classes en methodes, voor je het weet zit je een source-filter te schrijven als perligata.
Het gebruiken van een nederlandstalig datamodel valt buiten deze plugin. Hier valt veel voor te zeggen en is vaak zelfs belangrijk voor het beheersbaar houden van een project.
Lees verder ››