class RailsRestVoteGenerator

Public Class Methods

next_migration_number(dirname) click to toggle source
# File lib/generators/rails_rest_vote/rails_rest_vote_generator.rb, line 12
def self.next_migration_number(dirname)
  if ActiveRecord::Base.timestamped_migrations
    Time.new.utc.strftime("%Y%m%d%H%M%S")
  else
    "%.3d" % (current_migration_number(dirname) + 1)
  end
end
source_root() click to toggle source
# File lib/generators/rails_rest_vote/rails_rest_vote_generator.rb, line 8
def self.source_root
  @source_root ||= File.join(File.dirname(__FILE__), 'templates')
end

Public Instance Methods

create_migration_file() click to toggle source

copy vote migration file to host application from template folder.

# File lib/generators/rails_rest_vote/rails_rest_vote_generator.rb, line 21
def create_migration_file
  migration_template 'migration.rb', 'db/migrate/create_votes.rb'
end
create_model_file() click to toggle source

create vote model to host application

# File lib/generators/rails_rest_vote/rails_rest_vote_generator.rb, line 26
def create_model_file
  create_file 'app/models/vote.rb',vote_model
end
inject_model_content() click to toggle source

inject association in user.rb model of host application

has_many :votes

# File lib/generators/rails_rest_vote/rails_rest_vote_generator.rb, line 34
def inject_model_content
  content = model_contents

  class_path = if namespaced?
    class_name.to_s.split("::")
  else
    [class_name]
  end

  indent_depth = class_path.size - 1
  content = content.split("\n").map { |line| "  " * indent_depth + line } .join("\n") << "\n"

  inject_into_class(model_path, class_path.last, content) if model_exists?
end

Private Instance Methods

model_contents() click to toggle source
# File lib/generators/rails_rest_vote/rails_rest_vote_generator.rb, line 51
  def model_contents
    buffer = <<-CONTENT
  has_many :votes
  CONTENT
    buffer
  end
model_exists?() click to toggle source
# File lib/generators/rails_rest_vote/rails_rest_vote_generator.rb, line 68
def model_exists?
  File.exists?(File.join(destination_root, model_path))
end
model_path() click to toggle source
# File lib/generators/rails_rest_vote/rails_rest_vote_generator.rb, line 72
def model_path
  @model_path ||= File.join("app", "models", "#{file_path}.rb")
end
vote_model() click to toggle source
# File lib/generators/rails_rest_vote/rails_rest_vote_generator.rb, line 58
  def vote_model
<<RUBY
    class Vote < ActiveRecord::Base
      belongs_to :#{singular_table_name}
      belongs_to :votable, :polymorphic =>true
      validates :votable_type, :votable_id, :presence => true
    end
RUBY
  end