Class: Frame::Generators::PagesGenerator

Inherits:
Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/frame/pages/pages_generator.rb

Instance Method Summary collapse

Methods inherited from Base

banner, source_root

Instance Method Details

#add_application_page_loaderObject



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/generators/frame/pages/pages_generator.rb', line 110

def add_application_page_loader
  inject_into_class "app/controllers/application_controller.rb", ApplicationController do '
  before_filter :get_variables

  def get_variables
@display_pages = Page.order("ordinal")
  end

'
  end
end

#add_root_routeObject



42
43
44
# File 'lib/generators/frame/pages/pages_generator.rb', line 42

def add_root_route
  insert_into_file 'config/routes.rb', "  root :to => 'pages#show', :id => 0\n  get 'sessions/new'\n", :after => "#{Rails.application.class.parent_name}::Application.routes.draw do\n"
end

#add_scopesObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/generators/frame/pages/pages_generator.rb', line 83

def add_scopes
    #run "bundle"

    string='
  validates_presence_of :name, :location

  scope :topbar, where(:location => "topbar")
  scope :sidebar, where(:location => "sidebar")
  scope :userbar, where(:location => "userbar")
  scope :bottombar, where(:location => "bottombar")

'
    insert_into_file 'app/models/page.rb', string, :after => "class Page < ActiveRecord::Base\n"

#      inject_into_class "app/models/page.rb", Page do
#      '
#      validates_presence_of :name
#
#      scope :topbar, where(:location => "topbar")
#      scope :sidebar, where(:location => "sidebar")
#      scope :userbar, where(:location => "userbar")
#      scope :bottombar, where(:location => "bottombar")
#
#'
#      end
end

#create_pages_partialsObject



69
70
71
72
73
74
75
# File 'lib/generators/frame/pages/pages_generator.rb', line 69

def create_pages_partials
  template('app/views/pages/_form.html.erb')
  template('app/views/pages/_links.html.erb')
  template('app/views/pages/_page.html.erb')
  template('app/views/shared/_error_messages.html.erb')
  template('app/views/shared/_sidebar.html.erb')
end

#create_pages_viewsObject



77
78
79
80
81
# File 'lib/generators/frame/pages/pages_generator.rb', line 77

def create_pages_views
  template('app/views/layouts/pages.html.erb')
  template('app/views/pages/index.html.erb')
  template('app/views/pages/show.html.erb')
end

#generate_scaffoldObject



34
35
36
37
38
39
40
# File 'lib/generators/frame/pages/pages_generator.rb', line 34

def generate_scaffold
  generate("scaffold", "Page name:string title:string location:string content:text url:string ordinal:integer")
  append_to_file 'db/seeds.rb' do
    "\nPage.create(:name => 'Home', :title => 'Welcome to Home!', :content => '<h2>Content</h2>This is the content of the home page...', :location => 'topbar', :url => '', :ordinal => '1')\n"
  end
  generate("scaffold_controller", "Page")
end

#remove_indexObject

def add_gems

#add_gem "mysql2"
gem("mysql2")
gem("activerecord-mysql2-adapter")
comment_lines 'Gemfile', /gem 'sqlite3'/
#run "bundle"
Bundler.with_clean_env do
  #run "gem install activerecord-mysql2-adapter"
  run "bundle"
end
#Bundler.install

end



27
28
29
30
31
32
# File 'lib/generators/frame/pages/pages_generator.rb', line 27

def remove_index
  filename="public/index.html"
  #if File.exists?(filename) and yes?("Would you like to remove '#{filename}'?")
    remove_file filename
  #end
end

#update_dbObject



122
123
124
125
126
127
# File 'lib/generators/frame/pages/pages_generator.rb', line 122

def update_db
  #if yes?("Would you like to migrate the database?")
    rake("db:migrate")
    rake("db:seed")
  #end
end

#update_page_controllerObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/generators/frame/pages/pages_generator.rb', line 46

def update_page_controller
  comment_lines 'app/controllers/pages_controller.rb', /@pages = Page.all/
  gsub_file 'app/controllers/pages_controller.rb', /def show\n\s+\@page \= Page\.find\(params\[\:id\]\)/ do
    "def show
    if params[:id] == 0
@page = Page.find_by_name('home') || Page.first
    else
@page = Page.find(params[:id])
    end"
  end
  gsub_file 'app/controllers/pages_controller.rb', /format.html # show.html.erb\n      format.json { render json: @page }/ do
    "# show.html.erb
if @page
  format.html
  format.json { render json: @page }
else
  format.html { redirect_to action: 'new' }
  format.json { render json: @page.errors, status: :unprocessable_entity }
end
  "
  end
end