Skip to content

Instantly share code, notes, and snippets.

@ziemekwolski
Created April 1, 2019 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ziemekwolski/3cc167d87bee32546542a2882fbc6cb4 to your computer and use it in GitHub Desktop.
Save ziemekwolski/3cc167d87bee32546542a2882fbc6cb4 to your computer and use it in GitHub Desktop.
Ruby model formatted
class User < ActiveRecord::Base
# == Constants ============================================================
GENDERS = [[‘Male’, ‘m’], [‘Female’, ’f’]].freeze
# == Attributes ===========================================================
# paperclip
attr_accessor :remove_logo
# == Extensions ===========================================================
# globalize required
translates :sector, :memo, :fallbacks_for_empty_translations => true
# paperclip required
has_attached_file :logo, styles: { thumb: ["64x64!", :png] }
# == Relationships ========================================================
has_attached_file :avatar, styles: {
square_100: ‘100x100#’,
square_300: ‘300x300#’
}
# association one-to-many
has_many :documents
# == Validations ==========================================================
validates: email, presence: true, uniqueness: true,
email_format: true
# == Scopes ===============================================================
# == Callbacks ============================================================
before_validation :normalize_name, on: :create
after_validation :set_location, on: [ :create, :update ]
# == Class Methods ========================================================
def self.from_the_class
"Hello, from a class method"
end
def self.for_select
all.collect{|u| [“#{u.name} (#{u.email})”, u.id]}
end
# == Instance Methods =====================================================
def main_method
method1
end
def from_an_instance
"Hello, from an instance method"
end
protected
def method1
puts "Hi this is #{self.class}"
end
private
def normalize_name
self.name = name.downcase.titleize
end
def set_location
self.location = LocationService.query(self)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment