Skip to content

Instantly share code, notes, and snippets.

@pecha7x
Last active November 24, 2016 09:47
Show Gist options
  • Save pecha7x/63d00f6de6cebec00cbccd0cfa0db4a2 to your computer and use it in GitHub Desktop.
Save pecha7x/63d00f6de6cebec00cbccd0cfa0db4a2 to your computer and use it in GitHub Desktop.
Some examples of RoR model code and Backbone view
class Person < ActiveRecord::Base
# include module for maintenance person.status
include Person::Status
# soft delete support for model
acts_as_paranoid
# logs all changes for model
audited associated_with: :company
has_associated_audits
# constant define
# BIRTHDAY_RANGE - define date range for validation birthday
BIRTHDAY_RANGE = (Time.now.years_ago(99).to_date)..(Time.now.years_ago(5).to_date)
# callback for notify users about a new person
after_create :notify_new_enquiry, if: -> { new_enquiry? }
# relation with Activity model. All related activities will be removed after remove person
has_many :activities, inverse_of: :person, dependent: :destroy
# validation for birthday attribute.
# it allow blank value or value that include in the range, else will be validation error and specific error message
validates :birthday, inclusion: {in: BIRTHDAY_RANGE, allow_blank: true, message: I18n.t('please enter a valid birth date', scope: 'models.person.errors')}
# scope for search all imported people
# (used squeel gem for facilities)
scope :imported, lambda {
where{ (request_id != nil) | (form_id != nil) }
}
# format date from created_at attribute
# make human_created_at a new copy of the method created_at_formatted
def human_created_at
I18n.localize(created_at, format: company.try(:datetime_format)) rescue nil
end
alias_method :created_at_formatted, :human_created_at
# overides save method for models
def save(*)
super
rescue => e
if external_id_attributes_changed?
self.new_external_id_attributes.each { |k, v| update_column k.to_sym, v }
Rails.logger.error "System Integration Error. Failed to save person ##{self.id}. Attributes: #{changes}. Exception: #{e.message}"
else
raise e
end
end
private
# inform all online users via websokets
def notify_new_enquiry
company.users.each do |user|
if user.online? && PersonPolicy.new(user, self).manage?
FayePublish.for_user user: user, data: self, serializer: NewEnquirySerializer
end
end
end
end
@SomeApp.module "UsersApp.Edit", (Edit, App, Backbone, Marionette, $, _) ->
class Edit.FormView extends App.Views.LayoutView
template: "users/edit/templates/form"
form: ->
title: @getTitle()
regions:
passwordRegion: '.passwords'
ui:
phoneNumber: '#phone_number'
bindings:
"[name=first_name]" : "first_name"
"[name=email]" : "email"
"[name=gender]" :
observe: "gender"
initialize: ($el) ->
$el.select2
data: App.references.gender
destroy: ($el) ->
$el.select2 "destroy"
afterUpdate: ($el, value) ->
$el.select2 "val", value
modelEvents:
"change:phone_type" : "togglePhoneNumber"
onRender: ->
@stickit()
@syncDesktopNotifications()
onShow: ->
@ui.phoneNumber.hidePhone(true) if @model.get('phone_private')
syncDesktopNotifications: ->
App.vent.trigger("user:sync:enabled_desktop_notifications")
class Edit.PasswordButtons extends App.Views.ItemView
template: "users/edit/_reset_password"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment