Class: WebsiteBuilderEngine::ArticlesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/website_builder_engine/articles_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/website_builder_engine/articles_controller.rb', line 70

def create
  @article = Article.new(params[:article])
  if params[:article][:filename].ends_with?(".html")
    params[:article][:filename].gsub!('.html','')
  end
  if params[:format] == 'html'
    p1 = HTMLToTextileParser.new
    p2 = HTMLToTextileParser.new
    p1.feed(params[:offerpage][:content])
    @offerpage.content = p1.to_textile
    p2.feed(params[:offerpage][:sidebar])
    @offerpage.sidebar = p2.to_textile
  end
  respond_to do |format|
    if @article.save
      format.html { redirect_to articles_path, notice: 'Article was successfully created.' }
    else
      format.html { render action: "new" }
    end
  end
end

#destroyObject



107
108
109
110
111
112
113
114
115
# File 'app/controllers/website_builder_engine/articles_controller.rb', line 107

def destroy
  @article = Article.find(params[:id])
  @article.destroy
  @settings = Setting.first
  FileUtils.remove_file("#{@file_path + @article.filename}.html", force = true)
  respond_to do |format|
    format.html { redirect_to articles_url }
  end
end

#editObject



65
66
67
68
# File 'app/controllers/website_builder_engine/articles_controller.rb', line 65

def edit
  @article = Article.find(params[:id])
  @articles = Article.all
end

#indexObject



40
41
42
43
44
45
46
# File 'app/controllers/website_builder_engine/articles_controller.rb', line 40

def index
  @articles = Article.all

  respond_to do |format|
    format.html # index.html.erb
  end
end

#newObject



55
56
57
58
59
60
61
62
63
# File 'app/controllers/website_builder_engine/articles_controller.rb', line 55

def new
  @article = Article.new
  @articles = Article.all
  @article.related_items = []
  @article.sidebar = @settings.sidebar if @settings.sidebar
  respond_to do |format|
    format.html # new.html.erb
  end
end

#publishObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/website_builder_engine/articles_controller.rb', line 6

def publish
  # create an article page
  @article = Article.find(params[:id])
  @related_items = Hash.new
  if @article.related_items
    @article.related_items.each do |id|
      item = Article.find(id)
      @related_items[item.title] = "#{item.filename}.html"
    end
  end
  @permalink = "http://#{@settings.domain}/#{@settings.articles_directory}/#{@article.filename}.html"
  @content = RedCloth.new(@article.content).to_html.html_safe
  @sidebar = RedCloth.new(@article.sidebar).to_html.html_safe if @article.sidebar
  article_page = render_to_string(:template => "website_builder_engine/templates/article.html.haml", :layout => false )
  FileUtils.makedirs(@file_path) unless File.exists?(@file_path)
  File.open("#{@file_path + @article.filename}.html", 'w') {|f| f.write(article_page) }
  @article.update_attribute(:published, true)
  # create (or recreate) the welcome page
  @articles = Article.where(published: true)
  @welcome_sidebar = RedCloth.new(@settings.sidebar).to_html.html_safe if @settings.sidebar
  @welcome_intro = RedCloth.new(@settings.about).to_html.html_safe if @settings.about
  welcome_page = render_to_string(:template => "website_builder_engine/templates/homepage.html.haml", :layout => false )
  File.open("#{@docroot_path}index.html", 'w') {|f| f.write(welcome_page) }
  # create a sitemap.xml page
  @offerpages = Offerpage.all
  sitemap_page = render_to_string(:template => "website_builder_engine/templates/sitemap.xml.haml", :layout => false )
  File.open("#{@docroot_path}sitemap.xml", 'w') {|f| f.write(sitemap_page) }
  # display list of articles when done
  respond_to do |format|
    format.html { redirect_to articles_path, notice: "Built a webpage for the article \"#{@article.title}\"" }
  end

end

#showObject



48
49
50
51
52
53
# File 'app/controllers/website_builder_engine/articles_controller.rb', line 48

def show
  @article = Article.find(params[:id])
  respond_to do |format|
    format.html # show.html.erb
  end
end

#updateObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/website_builder_engine/articles_controller.rb', line 92

def update
  @article = Article.find(params[:id])
  if params[:article][:filename].ends_with?(".html")
    params[:article][:filename].gsub!('.html','')
  end
  params[:article][:related_items] ||= []
  respond_to do |format|
    if @article.update_attributes(params[:article])
      format.html { redirect_to articles_path, notice: 'Article was successfully updated.' }
    else
      format.html { render action: "edit" }
    end
  end
end