Another day with merb and merb-assets
Posted by Nucc
I had a long morning with javascripts helpers. Two days ago, I had installed merb-0.9.0, because I have would try to probe what new features get. The main changes is the separation of merb and merb-gen. The scripts directory was removed, and we can generate models, controllers, … with merb-gen.
merb-gen project cd project merb-gen model user
It was long time, until I found this. I started to write some models, controllers, and views. And views… This cause my long-long morning. In merb, we can seperate the javascript code and the layout. It’s very important, when we have a lot of views with one common layout. Let’s look, how does it work… First, (it’s very important!!!) we need to set in config/init.rb
-
dependency "merb-assets"
It contains our javascript helpers. In merb we can set bundles, but this is another story. Next, we create the layout, it will be a very simple layout…
-
<html>
-
<head>
-
<title> Project </title>
-
<%= include_required_js %>
-
</head>
-
<body>
-
<%= catch_content :for_layout %>
-
</body>
-
</html>
include_required_js is a target, that will insert the
Let us look the Users/create view. In this view, we will use prototype framework.
-
<%- require_js "prototype" %>
-
-
<div id="content">
-
blablabla
-
</div>
And the result:
-
<html>
-
<head>
-
<title> Project </title>
-
<script src="/javascripts/prototype.js" type="text/javascript"></script>
-
</head>
-
<body>
-
<%= catch_content :for_layout %>
-
</body>
-
</html>
It was 4 hour in the morning, I downloaded the newest version from git, and the solution was dependency merb-assets. But works ![]()
Datamapper and transactions :(
Posted by Nucc
I have a new project idea, and I decided to create it by Merb and Datamapper. I’ve two model, Authentication and Profile model. The problem, DM doesn’t have transaction controlling, but it’s very necessary…
-
class Authentication < DataMapper::Base
-
property :email, :string
-
property :password, :string
-
-
belongs_to :profile
-
end
-
-
class Profile < DataMapper::Base
-
property :firstname, :string
-
property :lastname, :string
-
property :phone, :string
-
end
So we have an Authentications table, which contains email and password pairs and a pointer to a Profiles DB element. Let’s see a problem with Datamapper. There are no transaction controlling yet.
Put the case that, come a new visitor, and try to register. Add an email, password, and his profile. We create a Profile object with his params, and save it, because in Auth object we have to set saved object for profile pointer. The password not too secure, so Auth throw invalid exception, we can’t save it. There is a profile without auth. Okay, if Auth throw invalid message, delete profile. Is it solution? No.. If only you use this db, it may be a solution. But if sy create a pointer for your new profile, then database won’t delete your profile object because a DB element is pointing to it.
Okay, if we check validation before each model’s save method, it solves some problem, but good only for validation problems. Let’s see, how ActiveRecord solution. AR has transaction controlling. Every (the most) DB has transaction controlling, because it’s base. AR is capable throw Exception, when a save, or destroy method gets an error (DM only returns true or false). We do a transaction block, and if sg throw exception, we call a ROLLBACK, or if everything okay, we call COMMIT. There’s a schema:
-
def save!
-
raise "blablabla Save problem" unless save
-
true
-
end
-
-
def self.transaction(name, &block)
-
begin
-
send(START TRANSACTION)
-
yield
-
send("COMMIT")
-
rescue Exception=>e
-
send("ROLLBACK")
-
raise e
-
end
-
end
It’s only an idea, but what happens, when one transaction block contains another? If we do a commit inside another transaction, and the other transaction do a Rollback, then delete committed rows?