We have released Modularity 2 Show archive.org snapshot . It has many incompatible changes. See below for a script to migrate your applications automatically.
We now use traits with the vanilla include
method:
class Article < ActiveRecord::Base
include DoesTrashable
end
When your trait has parameters, use square brackets:
class Article < ActiveRecord::Base
include DoesStripFields[:name, :brand]
end
Note how you can now follow trait names by CTRL+clicking on them in RubyMine!
We agreed to now prefix traits with Does
and no longer suffix them with Trait
:
module DoesTrashable
as_trait do
..
end
end
This will nicely group traits alphabetically in your code folders.
Let's say you split User
into the traits "authentication" and "permissions". In Modularity 1 you now had to say:
class User < ActiveRecord::Base
does 'user/authentication'
does 'user/permissions'
end
Because Modularity 2 traits are vanilla modules, you can take advantage that you are already inside the User
module and use the traits without mentioning namespaces:
# app/models/user.rb
class User < ActiveRecord::Base
include DoesAuthentication # is actually User::DoesAuthentication
include DoesPermissions # is actually User::DoesPermissions
end
# app/models/user/does_authentication.rb
module User::DoesAuthentication
as_trait do
# methods, validations, etc. regarding usernames and passwords go here
end
end
# app/models/user/does_permissions.rb
module User::DoesPermissions
as_trait do
# methods, validations, etc. regarding contact information go here
end
end
If you have been using Modularity 1 with the does
syntax, the Modularity 2 gem provides a script to migrate your Ruby project automatically.
Make sure your project has tests and you have a backup of your files (or pushed your commits to Git)
Install Modularity 2+
From your project directory, do this:
find . -name "*.rb" | migrate-modularity1-to-modularity2
The script will rename your files and change your code. It will also syntax-check your files after conversion
(since the script is not perfect).
Check the diff to see what the script has done.
Run tests to see if everything still works.