Class: Amalgam::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/amalgam/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#copy_initializerObject



11
12
13
# File 'lib/generators/amalgam/install_generator.rb', line 11

def copy_initializer
  template "amalgam.rb", "config/initializers/amalgam.rb"
end

#copy_localeObject



15
16
17
18
# File 'lib/generators/amalgam/install_generator.rb', line 15

def copy_locale
  copy_file "../../../config/locales/en.yml", "config/locales/amalgam.en.yml"
  copy_file "../../../config/locales/zh-CN.yml", "config/locales/amalgam.zh-CN.yml"
end

#create_migration_filesObject



77
78
79
80
81
82
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/generators/amalgam/install_generator.rb', line 77

def create_migration_files
  time = Time.now.strftime('%Y%m%d%H%M%S').to_i
  create_file "db/migrate/#{time}_create_pages.rb" do
    <<-RUBY
class CreatePages < ActiveRecord::Migration
  def change
    create_table :pages do |t|
t.string :title, :null => false
t.string :path
t.string :slug, :null => false
t.integer :lft
t.integer :rgt
t.integer :parent_id

t.timestamps
    end
  end
end
    RUBY
  end

  time += 1
  create_file "db/migrate/#{time}_create_admin_users.rb" do
    <<-RUBY
class CreateAdminUsers < ActiveRecord::Migration
  def change
    create_table :admin_users do |t|

t.string :username
t.string :email
t.string :password_digest

t.timestamps
    end
  end
end
    RUBY
  end

  time += 1
  create_file "db/migrate/#{time}_create_attachments.rb" do
    <<-RUBY
class CreateAttachments < ActiveRecord::Migration
  create_table :attachments do |t|
    t.references :attachable ,:polymorphic => {:default => 'Page'}
    t.string :name
    t.string :file
    t.string :content_type
    t.string :original_filename
    t.string :description
    t.string :secure_token
    t.integer :file_size
    t.integer :position

    t.timestamps
  end
end
    RUBY
  end

  time += 1
  create_file "db/migrate/#{time}_create_groups.rb" do
    <<-RUBY
class CreateGroups < ActiveRecord::Migration
  def change
    create_table :groups do |t|

t.string :name

t.timestamps
    end
  end
end
    RUBY
  end

  time += 1
  create_file "db/migrate/#{time}_create_base_groups.rb" do
    <<-RUBY
class CreateBaseGroups < ActiveRecord::Migration
  def change
    create_table :base_groups do |t|

t.integer :group_id
t.string :groupable_type
t.integer :groupable_id

t.timestamps
    end
  end
end
    RUBY
  end
end

#extraObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/generators/amalgam/install_generator.rb', line 172

def extra
  inject_into_file 'app/controllers/application_controller.rb', :before => 'protect_from_forgery' do
    <<-RUBY
  include Amalgam::Authorities::Controllers::Helpers
    RUBY
  end

  create_file "app/controllers/pages_controller.rb" do
    <<-RUBY
class PagesController < ApplicationController
  include Amalgam::TemplateFinder
  include Amalgam::Editable

  def show
    @page = Page.where(:slug => params[:slug]).first
    raise ActiveRecord::RecordNotFound , "Couldn't find page with PATH=\#\{params[:slug]\}" if @page.blank?
    render template_for(@page)
  end
end
    RUBY
  end

  inject_into_file 'config/routes.rb', :after => "Application.routes.draw do\n" do
    <<-RUBY
  hierarchical_resource :pages
  mount Amalgam::Engine => "/"
  root :to => 'pages#show' , :defaults => {:slug => 'home'}
    RUBY
  end

  append_file 'db/seeds.rb' do
    <<-RUBY
AdminUser.create(:username => 'admin', :email => '[email protected]',:password => 'admin1234', :password_confirmation => 'admin1234')
    RUBY
  end
end

#init_admin_userObject



31
32
33
34
35
36
37
38
39
# File 'lib/generators/amalgam/install_generator.rb', line 31

def init_admin_user
  create_file "app/models/admin_user.rb" do
     <<-RUBY
class AdminUser < Amalgam::Authorities::Models::ActiveRecord
  attr_accessible :email, :username
end
    RUBY
  end
end

#init_pageObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/generators/amalgam/install_generator.rb', line 20

def init_page
  create_file "app/models/page.rb" do
     <<-RUBY
class Page < ActiveRecord::Base
  include Amalgam::Models::Page
  attr_accessible :parent_id, :prev_id ,:next_id ,:title ,:slug
end
    RUBY
  end
end

#inti_attachmentsObject



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
# File 'lib/generators/amalgam/install_generator.rb', line 41

def inti_attachments
  create_file "app/models/attachment.rb" do
     <<-RUBY
class Attachment < ActiveRecord::Base
  attr_accessible :attachable_id, :file, :name, :description, :position

  mount_uploader :file , AttachmentUploader
  validates_presence_of :file, :on => :create

  belongs_to :attachable, :polymorphic => true


  delegate :url, :to => :file

  def serializable_hash(options = nil)
    options ||= {}
    options[:methods] ||= []
    options[:methods] << :url
    super(options)
  end

  protected

  before_save :update_file_attributes
  def update_file_attributes
    if file.present? && file_changed?
self.content_type = file.file.content_type
self.file_size = file.file.size
self.original_filename = file.file.original_filename
    end
  end
end
    RUBY
  end
end