Module: Cms::Commands::Actions

Defined in:
lib/cms/commands/actions.rb

Constant Summary collapse

RAILS_GEMFILE_PATTERN =
/gem ["|']rails["|'],/

Instance Method Summary collapse

Instance Method Details

#add_route_to_end(route) ⇒ Object

Adds a route as the last file of the project. Assumes we are inside the rails app.



74
75
76
# File 'lib/cms/commands/actions.rb', line 74

def add_route_to_end(route)
  inject_into_file 'config/routes.rb', "\n  #{route}\n", {:before => /^end$/}
end

#comment_out_rails_in_gemfileObject



52
53
54
55
# File 'lib/cms/commands/actions.rb', line 52

def comment_out_rails_in_gemfile
  gsub_file "Gemfile", RAILS_GEMFILE_PATTERN, "# gem 'rails',", :verbose => false
  say_status :rails, "Commenting out Rails dependency."
end

#current_projectObject



21
22
23
# File 'lib/cms/commands/actions.rb', line 21

def current_project
  @project_name || File.basename(Dir.pwd)
end

#find_custom_blocksArray<String>

Returns a list of all models in the current project.

Returns:

  • (Array<String>)

    List of file names matching the models



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cms/commands/actions.rb', line 93

def find_custom_blocks
  file_pattern = "app/models/*.rb"
  model_files = Dir.glob(file_pattern)
  block_files = model_files.map do |f|
    content = IO.read(f)
    puts "Found\n#{content}"
    if content.match(/acts_as_content_block/)
      File.basename(f, ".rb")
    else
      nil
    end
  end
  block_files.compact
end

#generate_installation_scriptObject



9
10
11
12
13
# File 'lib/cms/commands/actions.rb', line 9

def generate_installation_script
  template 'install_generator.erb', "lib/generators/#{current_project}/install/install_generator.rb"
  template 'USAGE', "lib/generators/#{current_project}/install/USAGE"
  empty_directory "lib/generators/#{current_project}/install/templates"
end

#include_cms_moduleObject



15
16
17
18
19
# File 'lib/cms/commands/actions.rb', line 15

def include_cms_module
  inject_into_file "lib/#{current_project}/engine.rb", :after => "isolate_namespace #{module_class}\n" do
    "\t\tinclude Cms::Module\n"
  end
end

#install_cms_seed_dataObject



66
67
68
69
70
# File 'lib/cms/commands/actions.rb', line 66

def install_cms_seed_data
  # Copy from Gem
  copy_file File.expand_path(File.join(__FILE__, "../../../../db/browsercms.seeds.rb")), "db/browsercms.seeds.rb"
  append_to_file('db/seeds.rb', "\nrequire File.expand_path('../browsercms.seeds.rb', __FILE__)\n")
end

#install_migrationsObject



62
63
64
# File 'lib/cms/commands/actions.rb', line 62

def install_migrations
  rake 'cms:install:migrations'
end

#migration_with_name(name) ⇒ String

Find the first migration within the current project given a partial name:

i.e. create_turtles

Might find 20120314204817_create_turtles.rb

Parameters:

  • name (String)

    Partial file name. Don’t include the .rb at the end

Returns:

  • (String)

    Full path to the file, so you can do file manipulation on it.



84
85
86
87
88
89
# File 'lib/cms/commands/actions.rb', line 84

def migration_with_name(name)
  file_pattern = "db/migrate/*#{name}.rb"
  files = Dir.glob(file_pattern)
  fail "Couldn't find a migration file matching this pattern (#{file_pattern})'" if files.empty?
  File.absolute_path(files.first)
end

#module_classObject

i.e. BcmsWhatever



26
27
28
# File 'lib/cms/commands/actions.rb', line 26

def module_class
  current_project.camelize
end

#run_bundle_installObject

Runs ‘bundle install` inside the correct project directory (unless –skip_bundle was passed to the command)



31
32
33
34
35
# File 'lib/cms/commands/actions.rb', line 31

def run_bundle_install
  inside current_project do
    run "bundle install" unless options[:skip_bundle]
  end
end

#run_bundle_updateObject

Run ‘bundle update`, exiting if it doesn’t work.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cms/commands/actions.rb', line 38

def run_bundle_update
  return if options[:skip_bundle]
  inside current_project do
    result = run "bundle update"
    unless result
      puts "Check your Gemfile to ensure the dependencies are correct. Update them, then rerun the last command.".red
      exit(false)
    end
  end

end

#update_browsercms_gem_versionObject



57
58
59
60
# File 'lib/cms/commands/actions.rb', line 57

def update_browsercms_gem_version
  gsub_file "Gemfile", /gem ["|']browsercms.*$/, "gem \"browsercms\", \"#{Cms::VERSION}\"", :verbose=>false
  say_status :gemfile, "Update browsercms to v#{Cms::VERSION}"
end