Class: HaveAPI::ModelAdapters::ActiveRecord::Output

Inherits:
HaveAPI::ModelAdapter::Output show all
Defined in:
lib/haveapi/model_adapters/active_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HaveAPI::ModelAdapter::Output

#initialize

Constructor Details

This class inherits a constructor from HaveAPI::ModelAdapter::Output

Class Method Details

.used_by(action) ⇒ Object



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
135
# File 'lib/haveapi/model_adapters/active_record.rb', line 98

def self.used_by(action)
  action.meta(:object) do
    output do
      custom :url_params, label: 'URL parameters',
             desc: 'An array of parameters needed to resolve URL to this object'
      bool :resolved, label: 'Resolved', desc: 'True if the association is resolved'
    end
  end

  if %i(object object_list).include?(action.input.layout)
    clean = Proc.new do |raw|
      if raw.is_a?(String)
        raw.strip.split(',')
      elsif raw.is_a?(Array)
        raw
      else
        nil
      end
    end

    desc = <<END
A list of names of associated resources separated by a comma.
Nested associations are declared with '__' between resource names.
For example, 'user,node' will resolve the two associations.
To resolve further associations of node, use e.g. 'user,node__location',
to go even deeper, use e.g. 'user,node__location__environment'.
END

    action.meta(:global) do
      input do
        custom :includes, label: 'Included associations',
               desc: desc, &clean
      end
    end

    action.send(:include, Action::InstanceMethods)
  end
end

Instance Method Details

#[](name) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/haveapi/model_adapters/active_record.rb', line 142

def [](name)
  param = @context.action.output[name]
  v = @object.send(param.db_name)

  if v.is_a?(::ActiveRecord::Base)
    resourcify(param, v)
  else
    v
  end
end

#has_param?(name) ⇒ Boolean

Returns:



137
138
139
140
# File 'lib/haveapi/model_adapters/active_record.rb', line 137

def has_param?(name)
  param = @context.action.output[name]
  param && @object.respond_to?(param.db_name)
end

#metaObject



153
154
155
156
157
158
159
160
# File 'lib/haveapi/model_adapters/active_record.rb', line 153

def meta
  params = @context.action.resolve.call(@object)

  {
      url_params: params.is_a?(Array) ? params : [params],
      resolved: true
  }
end