Module: Resourceize::InstanceVariables

Included in:
ResourcesController
Defined in:
lib/resourceize/instance_variables.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/resourceize/instance_variables.rb', line 21

def self.included(base)

    base.send(:before_filter) do |controller|

        controller.instance_eval do
          if request.resource

            if request.action.collection?

              if request.resource.respond_to? :paginate
                @collection = request.resource.paginate({:page => params[:page]})
              else
                @collection = request.resource.all
              end

              mapper = :active_record if (defined?(ActiveRecord)  && request.resource.superclass == ActiveRecord::Base) #  TODO: WALK THE ENTIRE INHERITANCE CHAIN!!!
              mapper = :mongo_mapper  if (defined?(MongoMapper)   && request.resource.include?(MongoMapper::Document))

              keys = if mapper == :active_record
                request.resource.class.column_names
              elsif mapper == :mongo_mapper
                request.resource.keys.keys.map &:to_s
              end

              @column_names = keys

              instance_variable_set :"@#{request.collection_name}", @collection

            elsif request.action.single_object?

              # single object request.actions
              if request.action.new?
                @object = request.resource.new

              elsif request.action.create?
                @object = request.resource.new(params[:"#{request.resource_name}"])

              else

                begin
                  @object = request.resource.find(params[:id])
                rescue
                  @object = request.resource.find_by_permalink(params[:id])
                end

              end #~ single_object requested_actions

              meta = InstanceVariables.object_meta(@object)

              @label        = meta[:label]
              @label_method = meta[:label_method]
              @column_names = meta[:column_names]
              @title        = meta[:title]
              @description  = meta[:description]
              @keywords     = meta[:keywords]

              instance_variable_set :"@#{request.resource_name}", @object

            end #~ if elsif

          end #~ if request.resource
        end #~ instance_eval

    end #~ send :before_filter

end

.object_meta(object) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/resourceize/instance_variables.rb', line 89

def InstanceVariables.object_meta(object)

  mapper = :active_record if (defined?(ActiveRecord)  && object.is_a?(ActiveRecord::Base))
  mapper = :mongo_mapper  if (defined?(MongoMapper)   && object.class.include?(MongoMapper::Document))

  keys = if mapper == :active_record
    object.class.column_names
  elsif mapper == :mongo_mapper
    object.class.keys.keys.map &:to_s
  end

  label_method = if object.respond_to?(:to_label)
    "to_label"
  elsif object.respond_to? "label"
    "label"
  elsif keys.include? "name"
    "name"
  elsif keys.include? "title"
    "title"
  end

  label = (object.send(:"#{label_method}") if label_method) || ""
  title = label

  description = if keys.include? "excerpt"
    object.excerpt
  elsif keys.include? "description"
    object.description
  end

  keywords = if object.respond_to? :"keywords"
    object.keywords
  elsif object.respond_to? :"tag_list"
    object.tag_list
  end

  {
    :label        => label,
    :label_method => label_method,
    :column_names => keys,
    :title        => title,
    :description  => description,
    :keywords     => keywords
  }

end