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



158
159
160
# File 'app/models/topic.rb', line 158

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
# 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).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
          }
        end
      end
    end
  end
  {
    "success" => true,
    #"activeTopic" => {"topicname" => "av", "grouping" => "theme"}, #TODO: from session
    "gbtopics" => topics,
    "results" => topics.size
  }
end

.query_headerObject



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

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

.query_header_fileObject



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

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

.query_header_fnameObject

header template



131
132
133
# File 'app/models/topic.rb', line 131

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



101
102
103
# File 'app/models/topic.rb', line 101

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

#info_headerObject



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

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

#info_header_fileObject



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

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

#info_header_fnameObject

header template for each topic



144
145
146
# File 'app/models/topic.rb', line 144

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

#legendObject



118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/topic.rb', line 118

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



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

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

#legend_file_autoObject



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

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

#legend_fnameObject

Topic legend collection



106
107
108
# File 'app/models/topic.rb', line 106

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


162
163
164
165
166
167
168
169
170
171
172
# File 'app/models/topic.rb', line 162

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, searchbbox) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'app/models/topic.rb', line 179

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

#query_layers(ability, active_layers) ⇒ Object

TODO: 0.5s



190
191
192
193
194
# File 'app/models/topic.rb', line 190

def query_layers(ability, active_layers) #TODO: 0.5s
  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

#rolesObject



174
175
176
177
# File 'app/models/topic.rb', line 174

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