Read more

The new Modularity 2 syntax

Henning Koch
January 09, 2014Software engineer at makandra GmbH

We have released Modularity 2 Show archive.org snapshot . It has many incompatible changes. See below for a script to migrate your applications automatically.

There is no does method anymore

Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

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!

New naming convention for traits

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.

When using namespaced traits, you can take advantage of Ruby's constant lookup rules

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

Automatically migrate a project to Modularity 2

If you have been using Modularity 1 with the does syntax, the Modularity 2 gem provides a script to migrate your Ruby project automatically.

  1. Make sure your project has tests and you have a backup of your files (or pushed your commits to Git)

  2. Install Modularity 2+

  3. From your project directory, do this:

    find . -name "*.rb" | migrate-modularity1-to-modularity2
    
  4. 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).

  5. Check the diff to see what the script has done.

  6. Run tests to see if everything still works.

Henning Koch
January 09, 2014Software engineer at makandra GmbH
Posted by Henning Koch to makandra dev (2014-01-09 13:41)