Module: Link2::Support

Defined in:
lib/link2/support.rb

Class Method Summary collapse

Class Method Details

.extract_resource(resource) ⇒ Object

Extract resource based on resource or collecton of resources.

Usage/Examples:

extract_resource?(nil)
  # => nil

extract_resource?([])
  # => nil

extract_resource?(@post)
  # => @post

extract_resource?([@post_1, @post_2])
  # => @post_2


47
48
49
50
# File 'lib/link2/support.rb', line 47

def extract_resource(resource)
  resource.compact! if resource.is_a?(Array)
  [resource].flatten.last
end

.find_resource_class(arg) ⇒ Object

Get resource class based on name, object, or class.

Example/Usage:

resource_class(:post), resource_class(@post), resource_class(Post)
  # => Post, Post, Post


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/link2/support.rb', line 14

def find_resource_class(arg)
  if arg.is_a?(NilClass)
    nil
  elsif arg.is_a?(Symbol)
    resource_class_name = arg.to_s.singularize.camelize
    resource_class_name.constantize
  elsif arg.is_a?(Class)
    arg
  elsif arg.is_a?(Object)
    arg.class
  else
    arg
  end
rescue
  raise "No such class: #{resource_class_name}"
end

.human_name_for(object) ⇒ Object

Get the human name for an object/class - in both Rails 2.3 and Rails 3.

Usage/Examples:

human_name_for(CoolStuff)

  # => "Cool stuff"

...or something like that.


146
147
148
# File 'lib/link2/support.rb', line 146

def human_name_for(object)
  object.model_name.human rescue object.human_name
end

.record_class?(object_or_class) ⇒ Boolean

Check if a specified objec is a record class type.

Usage/Examples:

record_class?(ActiveRecord::Base)
  # => true

record_class?(String)
  # => false

Returns:

  • (Boolean)


116
117
118
119
120
# File 'lib/link2/support.rb', line 116

def record_class?(object_or_class)
  return false if object_or_class == NilClass || object_or_class.is_a?(NilClass)
  object_or_class = object_or_class.new if object_or_class.is_a?(Class)
  self.record_object?(object_or_class)
end

.record_collection?(collection_maybe) ⇒ Boolean

Check if passed object is an array for record instances, i.e. “collection”. THis assumes all objects in the array is of same kind; makes little sense with mixing different records kinds in a collection, and in such case any auto-detection is hard.

Usage/Examples:

record_collection?([])
  # => false

record_collection?(@post)
  # => false

record_collection?([@post])
  # => true

record_collection?([@post, @article])
  # => false

record_collection?([@post, @post])
  # => true

Returns:

  • (Boolean)


98
99
100
101
102
103
104
# File 'lib/link2/support.rb', line 98

def record_collection?(collection_maybe)
  if collection_maybe.present? && collection_maybe.is_a?(Array)
    collection_maybe.compact.all? { |object| record_object?(object) }
  else
    false
  end
end

.record_object?(object) ⇒ Boolean

Check if a specified objec is a record instance.

Usage/Examples:

record_object?(Post.new)  # if: Post < ActiveRecord::Base, or similar.
  # => true

record_object?(Post)
  # => false

Returns:

  • (Boolean)


132
133
134
# File 'lib/link2/support.rb', line 132

def record_object?(object)
  object.respond_to?(:new_record?)
end

.resource_identifier_class?(object) ⇒ Boolean

Check if the specified object is a valid resource identifier class. Used for detecting current resource based on controller, action, etc.

resource_identifier_class?(:string)
  # => false

resource_identifier_class?(:post)
  # => true

resource_identifier_class?(String)
  # => false

resource_identifier_class?(Post)
  # => true

resource_identifier_class?("")
  # => false

resource_identifier_class?(@post)
  # => true

Returns:

  • (Boolean)


73
74
75
# File 'lib/link2/support.rb', line 73

def resource_identifier_class?(object)
  (object.is_a?(Symbol) || self.record_class?(object))
end