module Hobo::Model::ClassMethods

Attributes

creator_attribute[RW]

TODO: should this be an inheriting_cattr_accessor as well? Probably.

Public Instance Methods

children(*args) click to toggle source
# File lib/hobo/model.rb, line 337
def children(*args)
  view_hints.children *args
end
creator_type() click to toggle source
# File lib/hobo/model.rb, line 250
def creator_type
  attr_type(creator_attribute)
end
field_added(name, type, args, options) click to toggle source
# File lib/hobo/model.rb, line 105
def field_added(name, type, args, options)
  self.name_attribute            = name.to_sym if options.delete(:name)
  self.primary_content_attribute = name.to_sym if options.delete(:primary_content)
  self.creator_attribute         = name.to_sym if options.delete(:creator)
  validate = options.delete(:validate) {true}

  #FIXME - this should be in Hobo::Model::UserBase
  send(:login_attribute=, name.to_sym, validate) if options.delete(:login) && respond_to?(:login_attribute=)
end
find(*args) click to toggle source
# File lib/hobo/model.rb, line 237
def find(*args)
  result = super
  result.member_class = self if result.is_a?(Array)
  result
end
find_by_sql(*args) click to toggle source
# File lib/hobo/model.rb, line 244
def find_by_sql(*args)
  result = super
  result
end
has_inheritance_column?() click to toggle source
# File lib/hobo/model.rb, line 302
def has_inheritance_column?
  columns_hash.include?(inheritance_column)
end
inline_booleans(*args) click to toggle source
# File lib/hobo/model.rb, line 341
def inline_booleans(*args)
  view_hints.inline_booleans *args
end
method_missing(name, *args, &block) click to toggle source
# File lib/hobo/model.rb, line 307
def method_missing(name, *args, &block)
  name = name.to_s
  if create_automatic_scope(name)
    send(name.to_sym, *args, &block)
  else
    super(name.to_sym, *args, &block)
  end
end
named(*args) click to toggle source
# File lib/hobo/model.rb, line 99
def named(*args)
  raise NoNameError, "Model #{name} has no name attribute" unless name_attribute
  send("find_by_#{name_attribute}", *args)
end
never_show?(field) click to toggle source
# File lib/hobo/model.rb, line 232
def never_show?(field)
  (@hobo_never_show && field.to_sym.in?(@hobo_never_show)) || (superclass < Hobo::Model && superclass.never_show?(field))
end
respond_to?(method, include_private=false) click to toggle source
# File lib/hobo/model.rb, line 317
def respond_to?(method, include_private=false)
  super || create_automatic_scope(method, true)
end
reverse_reflection(association_name) click to toggle source
# File lib/hobo/model.rb, line 261
def reverse_reflection(association_name)
  refl = reflections[association_name.to_s] or raise "No reverse reflection for #{name}.#{association_name}"
  return nil if refl.options[:conditions] || refl.options[:polymorphic]

  if refl.macro == :has_many && (self_to_join = refl.through_reflection)
    # Find the reverse of a has_many :through (another has_many :through)

    join_to_self  = reverse_reflection(self_to_join.name)
    join_to_other = refl.source_reflection
    other_to_join = self_to_join.klass.reverse_reflection(join_to_other.name)

    return nil if self_to_join.options[:conditions] || join_to_other.options[:conditions]

    refl.klass.reflections.values.find do |r|
      r.macro == :has_many &&
        !r.options[:conditions] &&
        !r.options[:scope] &&
        r.through_reflection == other_to_join &&
        r.source_reflection  == join_to_self
    end
  else
    # Find the :belongs_to that corresponds to a :has_one / :has_many or vice versa

    reverse_macros = case refl.macro
                     when :has_many, :has_one
                       [:belongs_to]
                     when :belongs_to
                       [:has_many, :has_one]
                     end

    refl.klass.reflections.values.find do |r|
      r.macro.in?(reverse_macros) &&
        r.klass >= self &&
        !r.options[:conditions] &&
        !r.options[:scope] &&
        r.foreign_key.to_s == refl.foreign_key.to_s
    end
  end
end
search_columns() click to toggle source
# File lib/hobo/model.rb, line 255
def search_columns
  column_names = columns.*.name
  SEARCH_COLUMNS_GUESS.select{|c| c.in?(column_names) }
end
table_exists?() click to toggle source
# File lib/hobo/model.rb, line 345
def table_exists?
  @table_exists_cache = super if @table_exists_cache.nil?
  @table_exists_cache
end
to_url_path() click to toggle source
# File lib/hobo/model.rb, line 322
def to_url_path
  "#{name.underscore.pluralize}"
end
typed_id() click to toggle source
# File lib/hobo/model.rb, line 327
def typed_id
  HoboFields.to_name(self) || name.underscore.gsub("/", "__")
end
view_hints() click to toggle source
# File lib/hobo/model.rb, line 332
def view_hints
  class_name = "#{name}Hints"
  class_name.safe_constantize or Object.class_eval("class #{class_name} < Hobo::Model::ViewHints; end; #{class_name}")
end