Sha256: 4527d456b1b4b856b004db8c11e7c37d062c3e529e00175bdc9316b124a987e9

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

class <%= class_name %> < ActiveRecord::Base

<% for attribute in attributes -%>
  validates_presence_of :<%= attribute.name %>
<% end -%>
  
  attr_accessible #Enable mass assignment for allowed fields manually in order to avoid security issues
    
  # --- Hobo Permissions --- #

  def creatable_by?(creator)
    true
  end

  def updatable_by?(updater, new)
    true
  end

  def deletable_by?(deleter)
    true
  end

  def viewable_by?(viewer, field)
    true
  end
  
  def editable_by?(editor)
    #whether the edit link should be made visible to 'editor' user
    #this is different from updatable_by? because user may have the permission to change some of the fields.
    #TODO: investigate how Hobo determines whether to show edit link or not -> Hobo fakes an edit to use updatable_by? by setting the field to 'Hobo:Undefined'
    true
  end  
  
    def same_fields?(other, *fields)
      return true if other.nil?

      fields = fields.flatten
      fields.all?{|f| self.send(f) == other.send(f)}
    end


    def only_changed_fields?(other, *changed_fields)
      return true if other.nil?

      changed_fields = changed_fields.flatten.*.to_s
      all_cols = self.class.columns.*.name - []
      all_cols.all?{|c| c.in?(changed_fields) || self.send(c) == other.send(c) }
    end
  
  protected
  def validate
    <% for attribute in attributes.select {|v| (v.name =~ /email$/ )} -%>
    errors.add(:<%= attribute.name %>, "is not valid (use abc@abc.com)") unless <%= attribute.name %> =~ /\A([-a-z0-9]+[\w\.\-\+]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
    <% end -%>
    
    <% for attribute in attributes.select {|v| (v.name =~ /(website|url)$/ )} -%>
    errors.add(:<%= attribute.name %>, "is not valid (use http://www.abc.com)") unless <%= attribute.name %> =~ /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
    <% end -%>
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
nileshtrivedi-lp_resource_builder-0.5.1 generators/scaffold_resource/templates/model.rb
nileshtrivedi-lp_resource_builder-0.5.3 generators/scaffold_resource/templates/model.rb
nileshtrivedi-safe_resource-0.5.3 generators/scaffold_resource/templates/model.rb