cirandas.net

ref: master

plugins/fbes/controllers/public/fbes_plugin/responsa_controller.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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
class FbesPlugin::ResponsaController < PublicController

  def initiatives
    @last_updated_on = params[:last_updated_on] || 24
    @last_updated_on = @last_updated_on.to_i.months.ago

    @enterprises = environment.enterprises.visible.enabled.
      joins(:articles).joins('left join products on products.profile_id = profiles.id').
      where('profiles.updated_at > ? OR products.updated_at > ? OR articles.updated_at > ?', @last_updated_on, @last_updated_on, @last_updated_on).
      where('lat IS NOT NULL AND lng IS NOT NULL').
      uniq.group('profiles.id')
    @consumers_coops = environment.communities.joins(:orders).
      where('orders_plugin_orders.updated_at > ?', @last_updated_on).
      where('lat IS NOT NULL AND lng IS NOT NULL').
      uniq.group('profiles.id')

    @results = @enterprises + @consumers_coops

    @json = @results.map do |r|
      r.description ||= if r.is_a? Enterprise then 'Empreendimento de Economia Solidária' else 'Grupo de Consumo Responsável' end
      {
        local_id: r.id,
        url: url_for(r.url),
        title: r.short_name(nil),
        description: r.description,
        lat: r.lat,
        lng: r.lng,
        address: [r.address, r.address_line2, r.district].select{ |f| f.present? }.join(', '),
        zip_code: r.zip_code,
        phone1: r.contact_phone,
        city: r.city,
        state: r.state,
        country: 'BR',
        created_at: r.created_at,
        updated_at: r.updated_at,
        avatar: if r.image then "#{environment.top_url}#{r.image.public_filename}" else nil end,
      }.tap{ |h| h.delete_if{ |k, v| k.nil? } }
    end

    render json: Oj.dump(@json, mode: :compat)
  end

  protected

  # inherit routes from core skipping use_relative_controller!
  def url_for options
    options[:controller] = "/#{options[:controller]}" if options.is_a? Hash and options[:controller] and not options[:controller].to_s.starts_with? '/'
    super options
  end
  helper_method :url_for

end