Class: Burp::PageModel

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
app/models/burp/page_model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {:path => '', :snippets => {}}) ⇒ PageModel

Returns a new instance of PageModel.



27
28
29
30
31
32
33
34
# File 'app/models/burp/page_model.rb', line 27

def initialize(values = {:path => '', :snippets => {}})
  
  values.each_entry do |key,value|
    self.send("#{key}=".to_sym,value) if self.respond_to?("#{key}=".to_sym)
  end

  @original_path = values[:original_path]
end

Instance Attribute Details

#meta_descriptionObject

Returns the value of attribute meta_description.



11
12
13
# File 'app/models/burp/page_model.rb', line 11

def meta_description
  @meta_description
end

#pathObject

Returns the value of attribute path.



11
12
13
# File 'app/models/burp/page_model.rb', line 11

def path
  @path
end

#snippetsObject

Returns the value of attribute snippets.



11
12
13
# File 'app/models/burp/page_model.rb', line 11

def snippets
  @snippets
end

#titleObject

Returns the value of attribute title.



11
12
13
# File 'app/models/burp/page_model.rb', line 11

def title
  @title
end

Class Method Details

.allObject



70
71
72
# File 'app/models/burp/page_model.rb', line 70

def self.all
  all_paths.map {|path| find(path) }
end

.all_pathsObject



66
67
68
# File 'app/models/burp/page_model.rb', line 66

def self.all_paths
  ((Dir.glob("#{Burp.content_directory}pages/**/*.html") + Dir.glob("#{Burp.content_directory}pages/**/*.json")).map {|path| File.dirname(path.gsub(Burp.content_directory+"pages/","/")).gsub("/#root",'/') }).uniq
end

.find(path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/burp/page_model.rb', line 36

def self.find(path)

  fixed_path = path == '/' ? '/#root' : path
  on_disk_path =  Burp.content_directory+"pages/" + fixed_path
  on_disk_path = on_disk_path.gsub('//','/')

  if File.directory?(on_disk_path) && File.exist?("#{on_disk_path}/page.json")
  
    data = {}
    Dir.glob(on_disk_path+"/*.html").each do |snippet_path|
      name = File.basename(snippet_path).split('.').first
      data[name.to_sym] = File.read(snippet_path).html_safe
    end
  
    page_data = File.exist?("#{on_disk_path}/page.json") ? JSON.parse(File.read("#{on_disk_path}/page.json")) : {}
  
    PageModel.new(:snippets => data, :title => page_data['title'], :meta_description => page_data['meta_description'], :path => path, :original_path => path)
  else
    nil
  end
end

.model_nameObject



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

def self.model_name
  ActiveModel::Name.new(PageModel,nil,"Page")
end

Instance Method Details

#idObject



62
63
64
# File 'app/models/burp/page_model.rb', line 62

def id
  to_param
end

#persisted?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'app/models/burp/page_model.rb', line 100

def persisted?
  !!@original_path
end

#removeObject



90
91
92
93
94
# File 'app/models/burp/page_model.rb', line 90

def remove
  raise "Path must start with a slash '/'" unless path.start_with?("/")
  remove_dir
  Burp::Util.commit("Removed #{self.path}")
end

#root_fixed_path(path = self.path) ⇒ Object



96
97
98
# File 'app/models/burp/page_model.rb', line 96

def root_fixed_path(path = self.path)
  path == '/' ? '/#root' : path
end

#saveObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/burp/page_model.rb', line 74

def save
  if valid?
    remove_dir
    remove_dir(@original_path) unless @original_path.blank?
    create_target_dir
    
    save_snippets
  
    Burp::Util.commit("Saved #{self.path}")
    
    true
  else
    false
  end
end

#to_paramObject



58
59
60
# File 'app/models/burp/page_model.rb', line 58

def to_param
  path == "/" ? "/$root" : path
end