Recently in programming Category
You can just do this:
when the row already exists in the database (detected by a unique-constraint violation) the values of that row are updated and saved, otherwise the new row is created.
My current solution is this:You use it like this:
Warning: You need mysql 5.1 so that LAST_INSERT_ID(id) works. (Mysql bug)
There is some repetition in it. Why not infer the i18n-key from the action? My approach:
The parameters should also be passed to the i18n key. So for the first button_to call the translation method is invoked with: _('CommentRating.actions.create',:rating => :good, :comment => @comment)
Some of these ideas are relatively easy to implement:
Requirements:
- Each model needs an class-method called 'actions'
- Each model needs an instance-method called 'actions'
- The actions-methods return an proxy (ActionProxy) that records the method name that is invoked on it (method name = action name)
- the proxy returns an information object (ActionInformation) that contains:
- the instance or the class of the model
- the action name
- the parameters
- the ActionInformation has methods that infer the i18n key as well as the url
Now this can be used like this:
Improvements to work on:
- infer method (:delete etc)
- dont merge the parameters into the url. make them accessible seperately so they can be extracted and posted via hidden fields inside a form.
- maybe redefine actions in the models to preprocess the parameters: define_action :rate => lambda{|*params| {:rating => params[1]} }
I'm using the haml plugin in my rails application. In haml you can output plaintext like this:
Since i never output plaintext in my view because i use i18n i decided to modify haml:
So instead of outputting plaintext, you supply a i18n-key and haml outputs the translated text that belongs to that key.
The modification is quick & dirty and just a few lines long:
Go to the haml precompiler.rb and find the render_tag method.
There should be a 'switch action' switch.
Add the following lines to the switch:
This assumes that '_' is the method that is used for i18n.
You can even append parameters:
This invokes _('my.i18n.key.here',:param1 => 'value')
The different types that are currently available to describe a data format:
Supported types
NumberSerializer: short, int, long (+unsigned: ushort etc.), float, double
uint :attribute_name, :min => 1145, :max => 67546StringSerializer: nullterminated(default) or fixed-length
string :attr_name, :length => 32MagicSerializer: throws exception when the magic value (in this case 1337) is not found at the pecific position
magic :magic_number, :value => 1337ArraySerializer: reads an array from the file. The data format of each entry can be specified.
array(:things,:length => :arr_length) do int :thing_id float :value end
Status
Experimental version.
Todo:
- Option to specify if format is big- or little-endian
- Store errors (eg: integer out of range) in an error hash like in active record
- Optional elements (if :someattr == 0, then :otherattr is not present)
- Skip sections (if :someattr == 0, then skip x bytes)




