class Schofield::Generators::Attributes
Public Class Methods
new()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 10 def initialize @attributes = [] @presence_validateds = [] @presence_validated_one_to_ones = [] @attachments = [] @required_attachments = [] @accessibles = [] @max_numbers = {} @max_lengths = {} @references_count = 0 end
Public Instance Methods
attached_files()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 93 def attached_files @attachments.map { |attribute| "\n has_attached_file :#{attribute.attachment_name}, {" + (attribute.image? ? image_attachment(name) : non_image_attachment(name)) + "\n }.merge(S3_ATTACHMENT_INFO)" }.join("\n") end
attachments?()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 77 def attachments? @attachments.any? end
attr_accessibles()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 105 def attr_accessibles @attr_accessibles ||= @accessibles.any? ? 'attr_accessible :' + @accessibles.join(', :') : '' end
attr_accessibles?()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 73 def attr_accessibles? attr_accessibles.present? end
attr_accessors()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 99 def attr_accessors @attr_accessors ||= (@attachments.any? ? 'attr_accessor :' + @attachments.map { |attribute| %w( _content_type _file_size ).map{ |suffix| "#{attribute.attachment_name}#{suffix}" } + (attribute.image? ? ["#{attribute.attachment_name}_dimensions"] : []) }.flatten.join(', :') : '') end
attr_accessors?()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 69 def attr_accessors? attr_accessors.present? end
each() { |a| ... }
click to toggle source
# File lib/generators/schofield/attributes.rb, line 57 def each @attributes.each { |a| yield a } end
join?()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 61 def join? @references_count == @attributes.select{ |a| a.name != 'position' }.count && @references_count == 2 end
new_attribute(column, one_to_one)
click to toggle source
# File lib/generators/schofield/attributes.rb, line 23 def new_attribute column, one_to_one attribute = Attribute.new column @attributes << attribute @references_count += 1 if attribute.reference? if attribute.validate_presence? if one_to_one then @presence_validated_one_to_ones << attribute else @presence_validateds << attribute end end if attribute.attachment? @attachments << attribute @required_attachments << attribute if attribute.required_attachment? end if attribute.max_number @max_numbers[attribute.max_number] ||= [] @max_numbers[attribute.max_number] << attribute.name end if attribute.max_length @max_lengths[attribute.max_length] ||= [] @max_lengths[attribute.max_length] << attribute.name end accessible_name = attribute.accessible_name @accessibles << accessible_name if accessible_name.present? attribute end
to_s_string()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 85 def to_s_string case when name? then 'name' when title? then 'title' when text? then 'text' end end
validations()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 81 def validations @validations ||= get_validations end
validations?()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 65 def validations? validations.any? end
Private Instance Methods
get_validations()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 121 def get_validations @validations = [] validate_presence validate_presence_unless_nested validate_length validate_numericality validate_email_address validate_phone_number validate_url validate_attachments @validations end
image_attachment(name)
click to toggle source
# File lib/generators/schofield/attributes.rb, line 186 def image_attachment name "\n :styles => { :admin => '96x78#' }," + "\n :path => 'images/:class_with_thumbnails/:id/:fingerprint.:extension'," + "\n :convert_options => { :admin => \"-colorspace RGB -strip \#{Rails.configuration.bodmin.image_quality}\" }" end
join_names(names)
click to toggle source
# File lib/generators/schofield/attributes.rb, line 117 def join_names names ":#{names.join(', :')}" end
method_missing(method, *args, &block)
click to toggle source
# File lib/generators/schofield/attributes.rb, line 196 def method_missing(method, *args, &block) if match_data = method.to_s.match(/^(.+)\?$/) self.find { |a| a.name == match_data[1] }.present? end end
names_matching(regex)
click to toggle source
# File lib/generators/schofield/attributes.rb, line 113 def names_matching regex self.select { |a| a.name.match(regex) }.map(&:name) end
non_image_attachment(name)
click to toggle source
# File lib/generators/schofield/attributes.rb, line 192 def non_image_attachment name "\n :path => 'downloads/:class/:id/:fingerprint.:extension'" end
validate_attachments()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 176 def validate_attachments @attachments.each do |attribute| name = attribute.attachment_name @validations << "validates_attachment_presence :#{name}" if attribute.required_attachment? @validations << "validates_attachment_content_type :#{name}, Rails.configuration.bodmin.image_content_type" if attribute.image? @validations << "validates_attachment_size :#{name}, :less_than => 10.megabytes" @validations << "validates_attachment_dimensions :#{name}, :width => 1..Infinity, :height => 1..Infinity" if attribute.image? end end
validate_email_address()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 158 def validate_email_address if (names = names_matching(/email_address$/)).any? @validations << "validates #{join_names(names)}, :email => true, :allow_nil => true" end end
validate_length()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 146 def validate_length @max_lengths.each do |length, names| @validations << "validates #{join_names(names)}, :length => { :maximum => #{length} }, :allow_nil => true" end end
validate_numericality()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 152 def validate_numericality @max_numbers.each do |number, names| @validations << "validates #{join_names(names)}, :numericality => { :less_than_or_equal_to => #{number} }, :allow_nil => true" end end
validate_phone_number()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 164 def validate_phone_number if (names = names_matching(/phone_number$/)).any? @validations << "validates #{join_names(names)}, :phone_number => true, :allow_nil => true" end end
validate_presence()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 134 def validate_presence if (names = @presence_validateds.map(&:actual_name)).any? @validations << "validates #{join_names(names)}, :presence => true" end end
validate_presence_unless_nested()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 140 def validate_presence_unless_nested if (names = @presence_validated_one_to_ones.map(&:actual_name)).any? @validations << "validates #{join_names(names)}, :presence => true, :unless => :nested" end end
validate_url()
click to toggle source
# File lib/generators/schofield/attributes.rb, line 170 def validate_url if (names = names_matching(/url$/)).any? @validations << "validates #{join_names(names)}, :url => true, :allow_nil => true" end end