Class: Newsletter::Design
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Newsletter::Design
- Includes:
- Deleteable
- Defined in:
- app/models/newsletter/design.rb
Class Method Summary collapse
-
.import(filename, design_name = nil) ⇒ Object
Import a design from a YAML file, Parameters: filename - path/name of file on filesystem design_name => rename design if already taken.
Instance Method Summary collapse
-
#base64_encoded_images ⇒ Object
returns the images as an array of base64 encoded strings.
-
#base_design_path(this_name = nil) ⇒ Object
returns the path to the base of the design’s files.
-
#export(filename = nil) ⇒ Object
Export a design’s data to a YAML file.
- #html_text ⇒ Object
- #html_text=(text) ⇒ Object
-
#images ⇒ Object
returns the image filenames inside a design.
-
#images_path(the_name = nil) ⇒ Object
returns where a design’s images should go, can override for ‘old_name’.
-
#import_images(image_imports) ⇒ Object
imports images from array of base64 encoded images.
-
#move_images ⇒ Object
move a design’s images on name change.
- #name=(new_name) ⇒ Object
-
#name_as_path(this_name = nil) ⇒ Object
returns a version of name that is nice for filesytem use.
- #save(*args) ⇒ Object
-
#view_path(this_name = nil) ⇒ Object
returns path to newsletter design for use in views and is the same for actual file.
Methods included from Deleteable
#delete, included, #is_deleted?, #undelete
Class Method Details
.import(filename, design_name = nil) ⇒ Object
Import a design from a YAML file, Parameters:
filename - path/name of file on filesystem
design_name => rename design if already taken
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/models/newsletter/design.rb', line 50 def self.import(filename,design_name=nil) raise "You must give a filename to import!" unless filename data = YAML.load_file(filename) design = nil transaction do data[:name] = design_name if design_name design = Design.create(:name => data[:name], :html_text => data[:html_text], :description => data[:description]) data[:areas].each do |area_data| Area.import(design,area_data) end data[:elements].each do |element_data| Element.import(design,element_data) end design.import_images(data[:images]) end raise "Error importing design: #{design.errors..join("\n ")}" unless design.valid? design end |
Instance Method Details
#base64_encoded_images ⇒ Object
returns the images as an array of base64 encoded strings
91 92 93 94 95 96 |
# File 'app/models/newsletter/design.rb', line 91 def base64_encoded_images images.map {|image| { name: image, data: Base64.encode64(File.binread(File.join(images_path,image))) }} end |
#base_design_path(this_name = nil) ⇒ Object
returns the path to the base of the design’s files
78 79 80 81 |
# File 'app/models/newsletter/design.rb', line 78 def base_design_path(this_name=nil) this_name ||= name File.join(::Newsletter.designs_path,'designs',name_as_path(this_name)) end |
#export(filename = nil) ⇒ Object
Export a design’s data to a YAML file.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/newsletter/design.rb', line 31 def export(filename=nil) filename = "#{::Newsletter.designs_path}/exports/#{name_as_path}-export.yaml" unless filename FileUtils.mkdir_p(File.dirname(filename)) File.open(filename,'w') do |file| YAML.dump( { :name => name, :html_text => html_text, :description => description, :areas => areas.collect{|area| area.export_fields}, :elements => elements.collect{|element| element.export_fields}, :images => base64_encoded_images },file) end end |
#html_text ⇒ Object
121 122 123 124 |
# File 'app/models/newsletter/design.rb', line 121 def html_text return @html_text if @html_text @html_text = read_design end |
#html_text=(text) ⇒ Object
126 127 128 |
# File 'app/models/newsletter/design.rb', line 126 def html_text=(text) @html_text = text end |
#images ⇒ Object
returns the image filenames inside a design
84 85 86 87 88 |
# File 'app/models/newsletter/design.rb', line 84 def images Dir.glob(File.join(images_path,'*.*')).map{|f| File.basename(f)} || [] rescue => e [] end |
#images_path(the_name = nil) ⇒ Object
returns where a design’s images should go, can override for ‘old_name’
99 100 101 102 |
# File 'app/models/newsletter/design.rb', line 99 def images_path(the_name=nil) the_name ||= name File.join('public','images',name_as_path(the_name)) end |
#import_images(image_imports) ⇒ Object
imports images from array of base64 encoded images
111 112 113 114 115 116 117 118 119 |
# File 'app/models/newsletter/design.rb', line 111 def import_images(image_imports) image_imports ||= [] FileUtils.mkdir_p(images_path) image_imports.each do |image| File.binwrite(File.join(images_path,image[:name]), Base64.decode64(image[:data]) ) end end |
#move_images ⇒ Object
move a design’s images on name change
105 106 107 108 |
# File 'app/models/newsletter/design.rb', line 105 def move_images return unless @old_name && @old_name != name FileUtils.mv(images_path(@old_name),images_path) end |
#name=(new_name) ⇒ Object
130 131 132 133 134 |
# File 'app/models/newsletter/design.rb', line 130 def name=(new_name) return if self[:name].eql?(new_name) @old_name = self[:name] unless @old_name self[:name] = new_name end |
#name_as_path(this_name = nil) ⇒ Object
returns a version of name that is nice for filesytem use
145 146 147 148 |
# File 'app/models/newsletter/design.rb', line 145 def name_as_path(this_name=nil) this_name = name unless this_name this_name.gsub(/[^a-zA-Z0-9-]/,'_') end |
#save(*args) ⇒ Object
136 137 138 139 140 141 142 |
# File 'app/models/newsletter/design.rb', line 136 def save(*args) transaction do move_design_on_name_change write_design super end end |
#view_path(this_name = nil) ⇒ Object
returns path to newsletter design for use in views and is the same for actual file
72 73 74 75 |
# File 'app/models/newsletter/design.rb', line 72 def view_path(this_name=nil) this_name ||= name File.join(base_design_path(this_name),'layout.html.erb') end |