Skip to content

Commit

Permalink
Clean up related to commit request model and controller [#99]
Browse files Browse the repository at this point in the history
1. Removed invalid indentation
2. Use the short File.read when reading files
3. Follow the CommitRequestProcess spec
4. Truncate database on each CommitRequest spec loop
  • Loading branch information
oleander committed Mar 26, 2012
1 parent 406e950 commit 75f3dd7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
6 changes: 4 additions & 2 deletions app/controllers/commit_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ def new
end

def create
request = params[:commit_request]
request = JSON.parse(params[:commit_request])
@commit_request = CommitRequest.new(request.merge({
repository: params[:repository_id],
user: current_user.id
}))

flash[:notice] = "Commit request was created" if @commit_request.valid?
if @commit_request.save
flash[:notice] = "Commit request was created"
end
respond_with(repository, @commit_request)
end

Expand Down
30 changes: 21 additions & 9 deletions app/models/commit_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,37 @@ class CommitRequest
# @return String Commit message provided by frontend
#
def commit_message
if (defined?(@commit_message)).nil?
@commit_message = "WebCommit: #{$command}"
if @commit_message.to_s.length.between?(6, 96)
@commit_message
else
@commit_message = @commit_message.length < 5 || @commit_message.length > 96 ? "WebCommit: #{@command}" : @commit_message
"WebCommit: #{@command}"
end
end

#
# Ads @options to beanstalkd
# @return Boolean True if all validations passes
# @return Boolean
# false when validation fails or @options has been pushed
# true when data has been pushed to beanstalkd
#
def save
return false unless valid?
if @command == 'add'
@files.each{ |file|
file[:data] = open(APP_CONFIG['tmp_upload_directory'] + file[:id], "rb") { |io| io.read }
}
if @command == "add"
@files.each do |file|
file_name = File.join(APP_CONFIG['tmp_upload_directory'], file["id"])
file[:raw] = File.read(file_name)
end
end
@options = {:command => @command, :user => @user, :repository => @repository, :branch => @branch, :commit_message => @commit_message, :files => @files}

@options = {
command: @command,
user: @user,
repository: @repository,
branch: @branch,
commit_message: @commit_message,
files: @files
}

@@cache[@options.to_s] ||= publish :commit, (@options || {}).merge({
callback: {
class: "CommitRequest",
Expand Down
30 changes: 21 additions & 9 deletions spec/models/commit_request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
describe CommitRequest do
before (:each) do
DatabaseCleaner.clean
lab = create(:lab)
student = create(:student)
lab_group = create(:lab_group, given_course: lab.given_course)
create(:student_registered_for_course, lab_groups: [lab_group], student: student)
repo = create(:lab_has_group, lab_group: lab_group, lab: lab).repository

lab_group = create(:lab_group, {
given_course: lab.given_course
})

create(:student_registered_for_course, {
lab_groups: [lab_group],
student: student
})

repo = create(:lab_has_group, {
lab_group: lab_group,
lab: lab
}).repository

@value = {
command: "move",
Expand Down Expand Up @@ -38,12 +50,12 @@
end

it "should fail with an invalid filename" do
@value[:command] = "add"
@value[:files] = [
{id: 123, to: "src/main\0.cpp"},
{id: 124, to: "src/lib<hej>.h"},
{id: 125, to: "src/lib|?.cpp"}
]
@value[:command] = "add"
@value[:files] = [
{id: 123, to: "src/main\0.cpp"},
{id: 124, to: "src/lib<hej>.h"},
{id: 125, to: "src/lib|?.cpp"}
]
end

it "should fail with an invalid command" do
Expand Down

0 comments on commit 75f3dd7

Please sign in to comment.