Module: Hobo::Model::ClassMethods

Defined in:
lib/hobo/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



307
308
309
310
311
312
313
314
# 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

Instance Attribute Details

#creator_attributeObject

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



89
90
91
# File 'lib/hobo/model.rb', line 89

def creator_attribute
  @creator_attribute
end

Instance Method Details

#children(*args) ⇒ Object



337
338
339
# File 'lib/hobo/model.rb', line 337

def children(*args)
  view_hints.children *args
end

#creator_typeObject



250
251
252
# File 'lib/hobo/model.rb', line 250

def creator_type
  attr_type(creator_attribute)
end

#field_added(name, type, args, options) ⇒ Object



105
106
107
108
109
110
111
112
113
# 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) ⇒ Object



237
238
239
240
241
# 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) ⇒ Object



244
245
246
247
# File 'lib/hobo/model.rb', line 244

def find_by_sql(*args)
  result = super
  result
end

#has_inheritance_column?Boolean

Returns:



302
303
304
# File 'lib/hobo/model.rb', line 302

def has_inheritance_column?
  columns_hash.include?(inheritance_column)
end

#inline_booleans(*args) ⇒ Object



341
342
343
# File 'lib/hobo/model.rb', line 341

def inline_booleans(*args)
  view_hints.inline_booleans *args
end

#named(*args) ⇒ Object

Raises:



99
100
101
102
# 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) ⇒ Boolean

Returns:



232
233
234
# 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) ⇒ Boolean

Returns:



317
318
319
# 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) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# 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_columnsObject



255
256
257
258
# 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?Boolean

Returns:



345
346
347
348
# 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_pathObject



322
323
324
# File 'lib/hobo/model.rb', line 322

def to_url_path
  "#{name.underscore.pluralize}"
end

#typed_idObject



327
328
329
# File 'lib/hobo/model.rb', line 327

def typed_id
  HoboFields.to_name(self) || name.underscore.gsub("/", "__")
end

#view_hintsObject



332
333
334
335
# 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