Module: SData::ActiveRecordExtensions::Mixin::InstanceMethods

Defined in:
lib/s_data/active_record_extensions/mixin.rb

Instance Method Summary collapse

Instance Method Details

#resource_header_attributes(dataset, included) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/s_data/active_record_extensions/mixin.rb', line 106

def resource_header_attributes(dataset, included)
  hash = {}
  hash.merge!({"sdata:key" => self.id, "sdata:url" => self.sdata_resource_url(dataset)}) if self.id
  hash.merge!("sdata:descriptor" => self.entry_content) if included.include?("$descriptor")
  hash.merge!("sdata:uuid" => self.uuid.to_s) if self.respond_to?("uuid") && !self.uuid.blank?
  hash
end

#sdata_nameObject



94
95
96
# File 'lib/s_data/active_record_extensions/mixin.rb', line 94

def sdata_name
  self.class.name.demodulize
end

#sdata_node_name(entity = self.class) ⇒ Object



98
99
100
# File 'lib/s_data/active_record_extensions/mixin.rb', line 98

def sdata_node_name(entity=self.class)
  self.class.sdata_node_name(entity)
end

#sdata_resource_url(dataset) ⇒ Object



102
103
104
# File 'lib/s_data/active_record_extensions/mixin.rb', line 102

def sdata_resource_url(dataset)
  self.class.sdata_resource_kind_url(dataset) + "('#{self.id}')"
end

#to_atom(params = {}, opts = {}) ⇒ Object



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
87
88
89
90
91
92
# File 'lib/s_data/active_record_extensions/mixin.rb', line 44

def to_atom(params={}, opts={})
  opts = {
    :atom_show_categories => true,
    :atom_show_links => true,
    :atom_show_authors => true
  }.merge(opts)
  maximum_precedence = (!params[:precedence].blank? ? params[:precedence].to_i : 100)
  included = params[:include].to_s.split(',')
  selected = params[:select].to_s.split(',')
  dataset = params[:dataset] #Maybe || '-' but I don't think it's a good idea to imply a default dataset at this level
  
  query = "?#{clean_params(params).to_query}".chomp('?')
  sync = (params[:sync].to_s == 'true')
  expand = ((sync || included.include?('$children')) ? :all_children : :immediate_children)
  
  returning Atom::Entry.new do |entry|
    entry.id = self.sdata_resource_url(dataset)
    entry.title = entry_title
    entry.updated = self.class.sdata_date(self.updated_at)
    entry.authors << Atom::Person.new(:name => self.respond_to?('author') ? self.author : sdata_default_author)  if opts[:atom_show_authors]
    entry.links << Atom::Link.new(:rel => 'self', 
                                  :href => "#{self.sdata_resource_url(dataset)}#{query}", 
                                  :type => 'application/atom+xml; type=entry', 
                                  :title => 'Refresh') if opts[:atom_show_links] 
    entry.categories << Atom::Category.new(:scheme => 'http://schemas.sage.com/sdata/categories',
                                             :term   => self.sdata_node_name,
                                             :label  => self.sdata_node_name.underscore.humanize.titleize) if opts[:atom_show_categories] 
                                           
    yield entry if block_given?
  
    if maximum_precedence > 0
      begin
        payload = Payload.new(:included => included, 
                                :selected => selected, 
                                :maximum_precedence => maximum_precedence, 
                                :sync => sync,
                                :contract => self.sdata_contract_name,
                                :entity => self,
                                :expand => expand,
                                :dataset => dataset)
        payload.generate!
        entry.sdata_payload = payload
      rescue Exception => e
        entry.diagnosis = Atom::Content::Diagnosis.new(ApplicationDiagnosis.new(:exception => e).to_xml(:entry))
      end
    end
    entry.content = sdata_content
  end
end