Class: Decidim::Geo::GeoDatasourceType

Inherits:
Api::Types::BaseObject
  • Object
show all
Includes:
SanitizeHelper
Defined in:
lib/decidim/api/geo_datasource_type.rb

Instance Method Summary collapse

Instance Method Details



88
89
90
91
# File 'lib/decidim/api/geo_datasource_type.rb', line 88

def banner_image
  return object.attached_uploader(:banner_image).url(only_path: true) if object.respond_to?(:banner_image)
  return object.attachments.first.url if object.respond_to?(:attachments) && object.attachments.first
end

#component_idObject



33
34
35
# File 'lib/decidim/api/geo_datasource_type.rb', line 33

def component_id
  object.component.id if object.respond_to?(:component) 
end

#coordinatesObject



93
94
95
# File 'lib/decidim/api/geo_datasource_type.rb', line 93

def coordinates
  {latitude: latitude, longitude: longitude} if has_coordinates?
end

#descriptionObject



60
61
62
63
# File 'lib/decidim/api/geo_datasource_type.rb', line 60

def description
  return truncate_translated(object.body) if object.respond_to?(:body)
  return truncate_translated(object.description) if object.respond_to?(:description)
end

#end_timeObject



101
102
103
# File 'lib/decidim/api/geo_datasource_type.rb', line 101

def end_time
  object.end_time if object.respond_to?(:end_time)
end

#geomObject



115
116
117
# File 'lib/decidim/api/geo_datasource_type.rb', line 115

def geom
  RGeo::GeoJSON.encode(object.shapedata.geom) if (object.respond_to?(:shapedata) && !object.shapedata.nil?)
end

#has_coordinates?Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/decidim/api/geo_datasource_type.rb', line 132

def has_coordinates?
  (
    has_geo_location? && 
    !location.latitude.nil? && 
    !location.longitude.nil?
  ) || (
    object.respond_to?(:latitude) &&
    object.respond_to?(:longitude) && 
    !object.latitude.nil? && 
    !object.longitude.nil?
  )
end

#has_geo_location?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/decidim/api/geo_datasource_type.rb', line 123

def has_geo_location?
  object.respond_to?(:decidim_geo_space_location)
end

#idObject



29
30
31
# File 'lib/decidim/api/geo_datasource_type.rb', line 29

def id
  object.id 
end

#latitudeObject



105
106
107
108
# File 'lib/decidim/api/geo_datasource_type.rb', line 105

def latitude
  return location.latitude if has_geo_location?
  object.latitude
end


45
46
47
# File 'lib/decidim/api/geo_datasource_type.rb', line 45

def link
  Decidim::ResourceLocatorPresenter.new(object).path(anchor: "DecidimGeo")
end

#locationObject



127
128
129
130
# File 'lib/decidim/api/geo_datasource_type.rb', line 127

def location
  return nil unless has_geo_location?
  object.decidim_geo_space_location
end

#longitudeObject



110
111
112
113
# File 'lib/decidim/api/geo_datasource_type.rb', line 110

def longitude
  return location.longitude if has_geo_location?
  object.longitude
end

#participatory_space_idObject



37
38
39
# File 'lib/decidim/api/geo_datasource_type.rb', line 37

def participatory_space_id
  return object.component.participatory_space_id if object.respond_to?(:component) 
end

#participatory_space_typeObject



41
42
43
# File 'lib/decidim/api/geo_datasource_type.rb', line 41

def participatory_space_type
  return object.component.participatory_space_type if object.respond_to?(:component) 
end

#scopeObject



119
120
121
# File 'lib/decidim/api/geo_datasource_type.rb', line 119

def scope
  object.scope if object.respond_to?(:scope)
end

#short_descriptionObject



54
55
56
57
58
# File 'lib/decidim/api/geo_datasource_type.rb', line 54

def short_description
  return truncate_translated(object.short_description, 250) if object.respond_to?(:short_description)
  return truncate_translated(object.body, 250) if object.respond_to?(:body)
  return truncate_translated(object.description, 250) if object.respond_to?(:description)
end

#start_timeObject



97
98
99
# File 'lib/decidim/api/geo_datasource_type.rb', line 97

def start_time
  object.start_time if object.respond_to?(:start_time) 
end

#titleObject



49
50
51
52
# File 'lib/decidim/api/geo_datasource_type.rb', line 49

def title
  return object.title if object.respond_to?(:title) 
  return object.name if object.respond_to?(:name) 
end

#truncate_translated(value, chars = 2800) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/decidim/api/geo_datasource_type.rb', line 65

def truncate_translated(value, chars=2800) 
  value.each do |key, v| 
    if v.is_a?(Hash) 
      value[key] = truncate_translated(v, chars)
    else
      value[key] = 
        Rails::Html::FullSanitizer.new.sanitize(
          Decidim::HtmlTruncation.new(
            Decidim::ContentProcessor.render(
              v
            ),
            max_length: 2800,
            tail: "…",
            count_tags: false,
            count_tail: false,
            tail_before_final_tag: false
          ).perform
        ).html_safe
    end
  end
  value
end

#typeObject



25
26
27
# File 'lib/decidim/api/geo_datasource_type.rb', line 25

def type
  object.class.name
end