Class: Topic

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/topic.rb

Overview

OpenLayers “layer” (WMS, etc.)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_print_disclaimerObject

print disclaimer



161
162
163
# File 'app/models/topic.rb', line 161

def self.default_print_disclaimer
  File.open(File.join(Rails.root, 'app', 'views', 'topics', '_print_disclaimer.txt'), 'r') { |f| f.read }
end

.list(app, current_ability, zone, wms_host) ⇒ Object

Structure for Topic selection



39
40
41
42
43
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
93
94
95
96
97
98
99
100
101
102
# File 'app/models/topic.rb', line 39

def self.list(app, current_ability, zone, wms_host)
  topics = []
  ActiveRecord::Base.silence do
    app.gbapplications_categories.includes(:category).each do |gbapplications_category|
      category = gbapplications_category.category
      unless category.nil?
        category_topics = category.topics.accessible_by(current_ability)
        category_topics = category_topics.includes(:organisation).includes(:bg_topic).includes(:overlay_topics) # optimize query performance
        category_topics = category_topics.select('topics.*,categories_topics.sort AS categories_topics_sort')

        topics += category_topics.collect do |topic|
          subtopics = category_topics.select{|t| t.parent_id == topic.id}.collect do |subtopic|
            {
              "subtopicname" => subtopic.name,
              "subtopictitle" => subtopic.sub_title,
              "categories_topics_sort" => subtopic['categories_topics_sort'].to_i
            }
          end
          categorysort = gbapplications_category.sort - 1000*gbapplications_category.gbapp_specific rescue nil

          tools = Tool.accessible_tools(topic, current_ability)

          keywords = topic.title.split + (topic.keywords || '').split(',').collect(&:strip)

          {
            "name" => topic.name,
            "title" => topic.title,
            "print_title" => topic.print_title,
            "icon" => "/images/custom/themekl-#{topic.name.downcase}.gif",
            "organisationtitle" => topic.organisation.try(:title),
            "organisationsort" => topic.organisation.try(:sort),
            "categorytitle" => category.title,
            "gbapp_specific" => gbapplications_category.gbapp_specific,
            "categorysort" => categorysort,
            "categories_topics_sort" => topic['categories_topics_sort'].to_i,
            "keywords" => keywords,
            "geoliongdd" => topic.geolion_gdd(zone),
            "parent_id" => topic.parent_id,
            "hassubtopics" => subtopics.size > 0,
            "subtopics" => subtopics,
            "missingpermission" => current_ability.cannot?(:show, topic),
            "tools" => tools,
            "ollayer_class" => topic.ollayer_class, #z.B. "WMS"
            "ollayer_type" => topic.ollayer_args, #z.B. { name: "NASA Global Mosaic", url: "http://wms.jpl.nasa.gov/wms.cgi", params: {layers: "modis,global_mosaic"} }
            "bg_topic" => topic.bg_topic.try(:name),
            "overlay_topics" => topic.overlay_topics.collect(&:name),
            "wms_url" => "#{wms_host}/#{topic.name}",
            "background_layer" => topic.background_layer,
            "main_layer" => topic.main_layer,
            "overlay_layer" => topic.overlay_layer,
            "minscale" => topic.minscale,
            "gb1_params" => topic.gb1_params  #TODO: generalized param for custom viewers
          }
        end
      end
    end
  end
  {
    "success" => true,
    #"activeTopic" => {"topicname" => "av", "grouping" => "theme"}, #TODO: from session
    "gbtopics" => topics,
    "results" => topics.size
  }
end

.query_headerObject



142
143
144
# File 'app/models/topic.rb', line 142

def self.query_header
  @query_header ||= File.exist?(query_header_file) ? "topics/#{query_header_fname[1..-10]}" : nil
end

.query_header_fileObject



138
139
140
# File 'app/models/topic.rb', line 138

def self.query_header_file
  File.join(Rails.root, 'app', 'views', 'topics', query_header_fname)
end

.query_header_fnameObject

header template



134
135
136
# File 'app/models/topic.rb', line 134

def self.query_header_fname
  "_query_header.html.erb"
end

Instance Method Details

#category_enumObject

Enum for RailsAdmin form



25
26
27
# File 'app/models/topic.rb', line 25

def category_enum
  Category.all.collect {|p| [ p.name, p.id ] }
end

#geolion_gdd(site) ⇒ Object

Enum for RailsAdmin form (causes exception in name search) def parent_enum

Topic.all.collect {|p| [ p.name, p.id ] }

end



34
35
36
# File 'app/models/topic.rb', line 34

def geolion_gdd(site)
  site == SITE_DEFAULT ? geolion_gdd_internet : geolion_gdd_intranet
end

#icon_fnameObject



104
105
106
# File 'app/models/topic.rb', line 104

def icon_fname
  "themekl-#{name.downcase}.gif"
end

#info_headerObject



155
156
157
# File 'app/models/topic.rb', line 155

def info_header
  @info_header ||= File.exist?(info_header_file) ? "topics/custom/#{info_header_fname[1..-10]}" : nil
end

#info_header_fileObject



151
152
153
# File 'app/models/topic.rb', line 151

def info_header_file
  File.join(Rails.root, 'app', 'views', 'topics', 'custom', info_header_fname)
end

#info_header_fnameObject

header template for each topic



147
148
149
# File 'app/models/topic.rb', line 147

def info_header_fname
  "_#{name.downcase}_info.html.erb"
end

#legendObject



121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/topic.rb', line 121

def legend
  @legend ||= begin
    if File.exist?(legend_file)
      "topics/custom/#{legend_fname[1..-10]}"
    elsif File.exist?(legend_file_auto)
      "topics/custom/auto/#{legend_fname[1..-10]}"
    else
      nil
    end
  end
end

#legend_fileObject



113
114
115
# File 'app/models/topic.rb', line 113

def legend_file
  File.join(Rails.root, 'app', 'views', 'topics', 'custom', legend_fname)
end

#legend_file_autoObject



117
118
119
# File 'app/models/topic.rb', line 117

def legend_file_auto
  File.join(Rails.root, 'app', 'views', 'topics', 'custom', 'auto', legend_fname)
end

#legend_fnameObject

Topic legend collection



109
110
111
# File 'app/models/topic.rb', line 109

def legend_fname
  "_#{name.downcase}_legend.html.erb"
end


165
166
167
168
169
170
171
172
173
174
175
# File 'app/models/topic.rb', line 165

def print_disclaimer
  @disclaimer ||= begin
    fname = File.join(Rails.root, 'app', 'views', 'topics', 'custom', "_#{name.downcase}_print_disclaimer.txt")
    if File.exist?(fname)
      # use custom topic disclaimer if there exists a corresponding text file
      File.open(fname, 'r') { |f| f.read }
    else
      Topic.default_print_disclaimer
    end
  end
end

#query(ability, query_topic, search_geom, nearest = nil, user = nil, client_srid = nil) ⇒ Object



182
183
184
185
186
187
188
189
190
191
# File 'app/models/topic.rb', line 182

def query(ability, query_topic, search_geom, nearest=nil, user=nil, client_srid=nil)
  active_layers = query_topic['layers'].split(',')
  qlayers = query_layers(ability, active_layers)
  results = []
  qlayers.each do |layer|
    result = layer.query(ability, query_topic, search_geom, nearest, user, client_srid)
    results << result unless result.nil?
  end
  results
end

#query_layers(ability, active_layers) ⇒ Object

TODO: 0.5s



193
194
195
196
197
198
199
# File 'app/models/topic.rb', line 193

def query_layers(ability, active_layers) #TODO: 0.5s
  ActiveRecord::Base.silence do
    layers.accessible_by(ability).where('topics_layers.queryable').order('topics_layers.leg_sort DESC').find_all do |layer|
      active_layers.include?(layer.name)
    end
  end
end

#rolesObject



177
178
179
180
# File 'app/models/topic.rb', line 177

def roles
  perms = Permission.where(:resource_type => 'Topic', :resource => name).includes(:role)
  perms.collect(&:role)
end