Skip to content

Instantly share code, notes, and snippets.

@alekseyl
Created June 12, 2025 08:17
Show Gist options
  • Save alekseyl/dc2c357df1158b4ad1e49f66ed0c0220 to your computer and use it in GitHub Desktop.
Save alekseyl/dc2c357df1158b4ad1e49f66ed0c0220 to your computer and use it in GitHub Desktop.
ActiveRecord model structure latest revision
<% module_namespacing do -%>
class <%= class_name %> < <%= parent_class_name.classify %>
# Rails style guided+
#-------------------- 1. includes and extend --------------
#-------------------- 2. default scope --------------------
#-------------------- 3. inner classes --------------------
#-------------------- 4. constants ------------------------
#-------------------- 5. attr related macros --------------
#-------------------- 6. enums and related macroses -------
#-------------------- 7. scopes ---------------------------
#-------------------- 8. has and belongs ------------------
#-------------------- 9. accept nested macros ------------
#-------------------- 10. validation ----------------------
#-------------------- 11. before/after callbacks ----------
# Callback cheat sheet:
# - describes the callbacks order, including the around_* callbacks specifics: you must call yield inside
# - placing the (attr)_changed? / saved_change_to_(attr)? helpers according to their areas of applicability.
# It's better to copy the callback part whenever you add first one.
# 3.1 Creating an Object
# before_validation |
# after_validation |
# before_save |
# around_save | attr_changed? < yield >
# before_create | |
# around_create | attr_changed? < yield > saved_change_to_attr? |
# after_create saved_change_to_attr? |
# after_save |
# after_commit / after_rollback |
# 3.2 Updating an Object
# before_validation |
# after_validation |
# before_save |
# around_save | attr_changed? < yield >
# before_update | |
# around_update | attr_changed? < yield > saved_change_to_attr? |
# after_update saved_change_to_attr? |
# after_save |
# after_commit / after_rollback |
# 3.3 Destroying an Object
# before_destroy
# around_destroy
# after_destroy
# after_commit / after_rollback
#-------------------- 12. delegate macros -----------------
#-------------------- 13. other macros (like devise's) ----
#-------------------- should be placed after callbacks ----
#-------------------- 14. public class methods ------------
#-------------------- 15. instance public methods ---------
#-------------------- 16. protected and private methods ---
end
<% end -%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment