Module: QuickApi::Mongoid

Extended by:
ActiveSupport::Concern
Defined in:
lib/quick_api/mongoid.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#to_api(options = {relations: true}, result = {}) ⇒ Object



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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/quick_api/mongoid.rb', line 57

def to_api(options = {relations: true}, result = {})
  if options[:fields]
    api_fields = options[:fields]
  else
    api_fields = self.quick_api_attributes.nil? ? [] : self.quick_api_attributes
  end
  api_fields.each do |api_field|
    begin
      if (self.send(api_field)).class == Paperclip::Attachment
        picture_styles = []
        self.send(api_field).styles.each {|style| picture_styles << style[0]}
        result[api_field] = {original: "http://#{ActionMailer::Base.default_url_options[:host]}#{self.send(api_field).url}",
                             styles: picture_styles}
      else
        begin
          result[api_field] = self.send(api_field)
        rescue
          raise "The field #{api_field} don't exist in this Model"
        end
      end
    rescue
      begin
        result[api_field] = self.send(api_field)
      rescue
        raise "The field #{api_field} don't exist in this Model"
      end
    end
  end

  # if self.quick_api_methods
  #   result = api_method_options(result, self.quick_api_methods)
  # end 

  if options[:relations] == true
      result = api_many_options(result, self.quick_api_has_many)                if self.quick_api_has_many
      result = api_belongs_or_one_options(result, self.quick_api_belongs_to)    if self.quick_api_belongs_to
      result = api_belongs_or_one_options(result, self.quick_api_has_one)       if self.quick_api_has_one
      result = api_many_options(result, self.quick_api_has_and_belongs_to_many) if self.quick_api_has_and_belongs_to_many
      result = api_many_options(result, self.quick_api_embeds_many)             if self.quick_api_embeds_many
      result = api_belongs_or_one_options(result, self.quick_api_embedded_in)   if self.quick_api_embedded_in
      result = api_belongs_or_one_options(result, self.quick_api_embeds_one)    if self.quick_api_embeds_one
  end

  result = api_set_options(result, options)

  return result
end