Skip to content

Instantly share code, notes, and snippets.

@rocLv
Last active March 14, 2017 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rocLv/cd4d5da91fa1c99b82dc0ec7e1c1399f to your computer and use it in GitHub Desktop.
Save rocLv/cd4d5da91fa1c99b82dc0ec7e1c1399f to your computer and use it in GitHub Desktop.
# lib/templates/active_record/model/model.rb
<% module_namespacing do -%>
class <%= class_name %> < <%= parent_class_name.classify %>
# keep the default scope first (if any)
# constants come up next
# afterwards we put attr related macros
# followed by association macros
<% attributes.select(&:reference?).each do |attribute| -%>
belongs_to :<%= attribute.name %><%= ' , polymophic: true' if attribute.polymophic? %><%= ' , required: true' if attribute.required? %>
<% end -%>
# and validation macros
# next we have callbacks
# other mactors (like devise's) should be placed after the callbacks
<% if attributes.any?(&:password_digest?) -%>
has_secure_password
<% end -%>
# finaly, scopes
end
<% end -%>
@rocLv
Copy link
Author

rocLv commented Mar 14, 2017

Rails model best practice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment