summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/projects/snippets_controller.rb14
-rw-r--r--app/controllers/snippets_controller.rb2
-rw-r--r--app/models/ability.rb3
-rw-r--r--app/models/project.rb2
-rw-r--r--app/views/projects/snippets/_form.html.haml4
-rw-r--r--app/views/projects/snippets/edit.html.haml2
-rw-r--r--app/views/projects/snippets/index.html.haml2
-rw-r--r--app/views/projects/snippets/new.html.haml2
-rw-r--r--app/views/projects/snippets/show.html.haml2
-rw-r--r--config/routes.rb17
-rw-r--r--db/migrate/20130324172327_change_project_id_to_null_in_snipepts.rb9
-rw-r--r--db/schema.rb4
-rw-r--r--features/project/snippets.feature35
-rw-r--r--features/steps/project/project_snippets.rb100
-rw-r--r--features/steps/shared/paths.rb4
-rw-r--r--spec/factories.rb9
-rw-r--r--spec/features/projects/snippets_spec.rb101
-rw-r--r--spec/features/snippets_spec.rb99
18 files changed, 183 insertions, 228 deletions
diff --git a/app/controllers/projects/snippets_controller.rb b/app/controllers/projects/snippets_controller.rb
index 4602fbb989a..ebff5039ffa 100644
--- a/app/controllers/projects/snippets_controller.rb
+++ b/app/controllers/projects/snippets_controller.rb
@@ -27,12 +27,12 @@ class Projects::SnippetsController < Projects::ApplicationController
end
def create
- @snippet = @project.snippets.new(params[:snippet])
+ @snippet = @project.snippets.new(params[:project_snippet])
@snippet.author = current_user
@snippet.save
if @snippet.valid?
- redirect_to [@project, @snippet]
+ redirect_to project_snippet_path(@project, @snippet)
else
respond_with(@snippet)
end
@@ -42,10 +42,10 @@ class Projects::SnippetsController < Projects::ApplicationController
end
def update
- @snippet.update_attributes(params[:snippet])
+ @snippet.update_attributes(params[:project_snippet])
if @snippet.valid?
- redirect_to [@project, @snippet]
+ redirect_to project_snippet_path(@project, @snippet)
else
respond_with(@snippet)
end
@@ -58,7 +58,7 @@ class Projects::SnippetsController < Projects::ApplicationController
end
def destroy
- return access_denied! unless can?(current_user, :admin_snippet, @snippet)
+ return access_denied! unless can?(current_user, :admin_project_snippet, @snippet)
@snippet.destroy
@@ -81,11 +81,11 @@ class Projects::SnippetsController < Projects::ApplicationController
end
def authorize_modify_snippet!
- return render_404 unless can?(current_user, :modify_snippet, @snippet)
+ return render_404 unless can?(current_user, :modify_project_snippet, @snippet)
end
def authorize_admin_snippet!
- return render_404 unless can?(current_user, :admin_snippet, @snippet)
+ return render_404 unless can?(current_user, :admin_project_snippet, @snippet)
end
def module_enabled
diff --git a/app/controllers/snippets_controller.rb b/app/controllers/snippets_controller.rb
index a2e22a670a3..bb5fffca33c 100644
--- a/app/controllers/snippets_controller.rb
+++ b/app/controllers/snippets_controller.rb
@@ -60,7 +60,7 @@ class SnippetsController < ProjectResourceController
@snippet.destroy
- redirect_to project_snippets_path(@project)
+ redirect_to project_snippet_path(@project)
end
def raw
diff --git a/app/models/ability.rb b/app/models/ability.rb
index 41f7127403c..928b36e6c80 100644
--- a/app/models/ability.rb
+++ b/app/models/ability.rb
@@ -7,6 +7,7 @@ class Ability
when "Project" then project_abilities(user, subject)
when "Issue" then issue_abilities(user, subject)
when "Note" then note_abilities(user, subject)
+ when "ProjectSnippet" then project_snippet_abilities(user, subject)
when "Snippet" then snippet_abilities(user, subject)
when "MergeRequest" then merge_request_abilities(user, subject)
when "Group", "Namespace" then group_abilities(user, subject)
@@ -135,7 +136,7 @@ class Ability
end
- [:issue, :note, :snippet, :merge_request].each do |name|
+ [:issue, :note, :project_snippet, :snippet, :merge_request].each do |name|
define_method "#{name}_abilities" do |user, subject|
if subject.author == user
[
diff --git a/app/models/project.rb b/app/models/project.rb
index 23eb7f90194..a0f014a15ab 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -50,7 +50,7 @@ class Project < ActiveRecord::Base
has_many :milestones, dependent: :destroy
has_many :users_projects, dependent: :destroy
has_many :notes, dependent: :destroy
- has_many :snippets, dependent: :destroy
+ has_many :snippets, dependent: :destroy, class_name: "ProjectSnippet"
has_many :deploy_keys, dependent: :destroy, class_name: "Key", foreign_key: "project_id"
has_many :hooks, dependent: :destroy, class_name: "ProjectHook"
has_many :wikis, dependent: :destroy
diff --git a/app/views/projects/snippets/_form.html.haml b/app/views/projects/snippets/_form.html.haml
index 77162cdcde3..99a8761daef 100644
--- a/app/views/projects/snippets/_form.html.haml
+++ b/app/views/projects/snippets/_form.html.haml
@@ -2,7 +2,7 @@
= @snippet.new_record? ? "New Snippet" : "Edit Snippet ##{@snippet.id}"
%hr
.snippet-form-holder
- = form_for [@project, @snippet] do |f|
+ = form_for [@project, @snippet], as: :project_snippet, url: url do |f|
-if @snippet.errors.any?
.alert.alert-error
%ul
@@ -30,7 +30,7 @@
= f.submit 'Save', class: "btn-save btn"
= link_to "Cancel", project_snippets_path(@project), class: " btn"
- unless @snippet.new_record?
- .pull-right= link_to 'Destroy', [@project, @snippet], confirm: 'Are you sure?', method: :delete, class: "btn pull-right danger delete-snippet", id: "destroy_snippet_#{@snippet.id}"
+ .pull-right= link_to 'Destroy', project_snippet_path(@project, @snippet), confirm: 'Are you sure?', method: :delete, class: "btn pull-right danger delete-snippet", id: "destroy_snippet_#{@snippet.id}"
:javascript
diff --git a/app/views/projects/snippets/edit.html.haml b/app/views/projects/snippets/edit.html.haml
index f81c0b8bc64..e28b7d4937e 100644
--- a/app/views/projects/snippets/edit.html.haml
+++ b/app/views/projects/snippets/edit.html.haml
@@ -1 +1 @@
-= render "snippets/form"
+= render "projects/snippets/form", url: project_snippet_path(@project, @snippet)
diff --git a/app/views/projects/snippets/index.html.haml b/app/views/projects/snippets/index.html.haml
index bacf23d8f8d..12760d377f3 100644
--- a/app/views/projects/snippets/index.html.haml
+++ b/app/views/projects/snippets/index.html.haml
@@ -12,7 +12,7 @@
%th Title
%th File Name
%th Expires At
- = render @snippets
+ = render partial: "projects/snippets/snippet", collection: @snippets
- if @snippets.empty?
%tr
%td{colspan: 3}
diff --git a/app/views/projects/snippets/new.html.haml b/app/views/projects/snippets/new.html.haml
index f81c0b8bc64..460af34f676 100644
--- a/app/views/projects/snippets/new.html.haml
+++ b/app/views/projects/snippets/new.html.haml
@@ -1 +1 @@
-= render "snippets/form"
+= render "projects/snippets/form", url: project_snippets_path(@project, @snippet)
diff --git a/app/views/projects/snippets/show.html.haml b/app/views/projects/snippets/show.html.haml
index 12534edf8ba..e325c3d84e6 100644
--- a/app/views/projects/snippets/show.html.haml
+++ b/app/views/projects/snippets/show.html.haml
@@ -5,5 +5,5 @@
= link_to "Edit", edit_project_snippet_path(@project, @snippet), class: "btn btn-small pull-right", title: 'Edit Snippet'
%br
-%div= render 'blob'
+%div= render 'projects/snippets/blob'
%div#notes= render "notes/notes_with_form"
diff --git a/config/routes.rb b/config/routes.rb
index d87dd4abdb0..21a5eb9bfec 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -177,6 +177,14 @@ Gitlab::Application.routes.draw do
match "/compare/:from...:to" => "compare#show", as: "compare",
:via => [:get, :post], constraints: {from: /.+/, to: /.+/}
+ scope module: :projects do
+ resources :snippets do
+ member do
+ get "raw"
+ end
+ end
+ end
+
resources :wikis, only: [:show, :edit, :destroy, :create] do
collection do
get :pages
@@ -244,21 +252,12 @@ Gitlab::Application.routes.draw do
end
end
- scope module: :projects do
- resources :snippets do
- member do
- get "raw"
- end
- end
- end
-
resources :hooks, only: [:index, :create, :destroy] do
member do
get :test
end
end
-
resources :team, controller: 'team_members', only: [:index]
resources :milestones, except: [:destroy]
resources :labels, only: [:index]
diff --git a/db/migrate/20130324172327_change_project_id_to_null_in_snipepts.rb b/db/migrate/20130324172327_change_project_id_to_null_in_snipepts.rb
new file mode 100644
index 00000000000..4c992bac4d1
--- /dev/null
+++ b/db/migrate/20130324172327_change_project_id_to_null_in_snipepts.rb
@@ -0,0 +1,9 @@
+class ChangeProjectIdToNullInSnipepts < ActiveRecord::Migration
+ def up
+ change_column :snippets, :project_id, :integer, :null => true
+ end
+
+ def down
+ change_column :snippets, :project_id, :integer, :null => false
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 88e249047e6..f54bd2e16ea 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20130324151736) do
+ActiveRecord::Schema.define(:version => 20130324172327) do
create_table "events", :force => true do |t|
t.string "target_type"
@@ -185,7 +185,7 @@ ActiveRecord::Schema.define(:version => 20130324151736) do
t.string "title"
t.text "content"
t.integer "author_id", :null => false
- t.integer "project_id", :null => false
+ t.integer "project_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "file_name"
diff --git a/features/project/snippets.feature b/features/project/snippets.feature
new file mode 100644
index 00000000000..a26c8dc8474
--- /dev/null
+++ b/features/project/snippets.feature
@@ -0,0 +1,35 @@
+Feature: Project Snippets
+ Background:
+ Given I sign in as a user
+ And I own project "Shop"
+ And project "Shop" have "Snippet one" snippet
+ And project "Shop" have no "Snippet two" snippet
+ And I visit project "Shop" snippets page
+
+ Scenario: I should see snippets
+ Given I visit project "Shop" snippets page
+ Then I should see "Snippet one" in snippets
+ And I should not see "Snippet two" in snippets
+
+ Scenario: I create new project snippet
+ Given I click link "New Snippet"
+ And I submit new snippet "Snippet three"
+ Then I should see snippet "Snippet three"
+
+ @javascript
+ Scenario: I comment on a snippet "Snippet one"
+ Given I visit snippet page "Snippet one"
+ And I leave a comment like "Good snippet!"
+ Then I should see comment "Good snippet!"
+
+ Scenario: I update "Snippet one"
+ Given I visit snippet page "Snippet one"
+ And I click link "Edit"
+ And I submit new title "Snippet new title"
+ Then I should see "Snippet new title"
+
+ Scenario: I destroy "Snippet one"
+ Given I visit snippet page "Snippet one"
+ And I click link "Edit"
+ And I click link "Destroy"
+ Then I should not see "Snippet one" in snippets
diff --git a/features/steps/project/project_snippets.rb b/features/steps/project/project_snippets.rb
new file mode 100644
index 00000000000..c8580d6fd30
--- /dev/null
+++ b/features/steps/project/project_snippets.rb
@@ -0,0 +1,100 @@
+class ProjectSnippets < Spinach::FeatureSteps
+ include SharedAuthentication
+ include SharedProject
+ include SharedNote
+ include SharedPaths
+
+ And 'project "Shop" have "Snippet one" snippet' do
+ create(:project_snippet,
+ title: "Snippet one",
+ content: "Test content",
+ file_name: "snippet.rb",
+ project: project,
+ author: project.users.first)
+ end
+
+ And 'project "Shop" have no "Snippet two" snippet' do
+ create(:snippet,
+ title: "Snippet two",
+ content: "Test content",
+ file_name: "snippet.rb",
+ author: project.users.first)
+ end
+
+ Given 'I click link "New Snippet"' do
+ click_link "Add new snippet"
+ end
+
+ Given 'I click link "Snippet one"' do
+ click_link "Snippet one"
+ end
+
+ Then 'I should see "Snippet one" in snippets' do
+ page.should have_content "Snippet one"
+ end
+
+ And 'I should not see "Snippet two" in snippets' do
+ page.should_not have_content "Snippet two"
+ end
+
+ And 'I should not see "Snippet one" in snippets' do
+ page.should_not have_content "Snippet one"
+ end
+
+ And 'I click link "Edit"' do
+ within ".page_title" do
+ click_link "Edit"
+ end
+ end
+
+ And 'I click link "Destroy"' do
+ click_link "Destroy"
+ end
+
+ And 'I submit new snippet "Snippet three"' do
+ fill_in "project_snippet_title", :with => "Snippet three"
+ select "forever", :from => "project_snippet_expires_at"
+ fill_in "project_snippet_file_name", :with => "my_snippet.rb"
+ within('.file-editor') do
+ find(:xpath, "//input[@id='project_snippet_content']").set 'Content of snippet three'
+ end
+ click_button "Save"
+ end
+
+ Then 'I should see snippet "Snippet three"' do
+ page.should have_content "Snippet three"
+ page.should have_content "Content of snippet three"
+ end
+
+ And 'I submit new title "Snippet new title"' do
+ fill_in "project_snippet_title", :with => "Snippet new title"
+ click_button "Save"
+ end
+
+ Then 'I should see "Snippet new title"' do
+ page.should have_content "Snippet new title"
+ end
+
+ And 'I leave a comment like "Good snippet!"' do
+ within('.js-main-target-form') do
+ fill_in "note_note", with: "Good snippet!"
+ click_button "Add Comment"
+ end
+ end
+
+ Then 'I should see comment "Good snippet!"' do
+ page.should have_content "Good snippet!"
+ end
+
+ And 'I visit snippet page "Snippet one"' do
+ visit project_snippet_path(project, project_snippet)
+ end
+
+ def project
+ @project ||= Project.find_by_name!("Shop")
+ end
+
+ def project_snippet
+ @project_snippet ||= ProjectSnippet.find_by_title!("Snippet One")
+ end
+end
diff --git a/features/steps/shared/paths.rb b/features/steps/shared/paths.rb
index 1af8b478155..42004df426e 100644
--- a/features/steps/shared/paths.rb
+++ b/features/steps/shared/paths.rb
@@ -263,6 +263,10 @@ module SharedPaths
visit project_wiki_path(@project, :home)
end
+ Given 'I visit project "Shop" snippets page' do
+ visit project_snippets_path(project)
+ end
+
def root_ref
@project.repository.root_ref
end
diff --git a/spec/factories.rb b/spec/factories.rb
index 41766859468..67caec9f851 100644
--- a/spec/factories.rb
+++ b/spec/factories.rb
@@ -196,7 +196,7 @@ FactoryGirl.define do
user
end
- factory :snippet do
+ factory :project_snippet do
project
author
title
@@ -204,6 +204,13 @@ FactoryGirl.define do
file_name
end
+ factory :snippet do
+ author
+ title
+ content
+ file_name
+ end
+
factory :protected_branch do
name
project
diff --git a/spec/features/projects/snippets_spec.rb b/spec/features/projects/snippets_spec.rb
deleted file mode 100644
index 3c85359ee06..00000000000
--- a/spec/features/projects/snippets_spec.rb
+++ /dev/null
@@ -1,101 +0,0 @@
-require 'spec_helper'
-
-describe "Project::Snippets" do
- let(:project) { create(:project) }
-
- before do
- login_as :user
- project.team << [@user, :developer]
- end
-
- describe "GET /:project/snippets" do
- before do
- @snippet = create(:snippet,
- author: @user,
- project: project)
-
- visit project_snippets_path(project)
- p project_snippets_path(project)
-
- end
-
- subject { page }
-
- it { should have_content(@snippet.title[0..10]) }
- it { should have_content(@snippet.project.name) }
-
- describe "Destroy" do
- before do
- # admin access to remove snippet
- @user.users_projects.destroy_all
- project.team << [@user, :master]
- visit edit_project_snippet_path(project, @snippet)
- end
-
- it "should remove entry" do
- expect {
- click_link "destroy_snippet_#{@snippet.id}"
- }.to change { Snippet.count }.by(-1)
- end
- end
- end
-
- describe "New project snippet" do
- before do
- visit project_snippets_path(project)
- click_link "New Snippet"
- end
-
- it "should open new snippet popup" do
- page.current_path.should == new_project_snippet_path(project)
- end
-
- describe "fill in", js: true do
- before do
- fill_in "snippet_title", with: "login function"
- fill_in "snippet_file_name", with: "test.rb"
- page.execute_script("editor.insert('def login; end');")
- end
-
- it { expect { click_button "Save" }.to change {Snippet.count}.by(1) }
-
- it "should add new snippet to table" do
- click_button "Save"
- page.current_path.should == project_snippet_path(project, Snippet.last)
- page.should have_content "login function"
- page.should have_content "test.rb"
- end
- end
- end
-
- describe "Edit project snippet" do
- before do
- @snippet = create(:snippet,
- author: @user,
- project: project)
- visit project_snippet_path(project, @snippet)
- click_link "Edit Snippet"
- end
-
- it "should open edit page" do
- page.current_path.should == edit_project_snippet_path(project, @snippet)
- end
-
- describe "fill in" do
- before do
- fill_in "snippet_title", with: "login function"
- fill_in "snippet_file_name", with: "test.rb"
- end
-
- it { expect { click_button "Save" }.to_not change {Snippet.count} }
-
- it "should update snippet fields" do
- click_button "Save"
-
- page.current_path.should == project_snippet_path(project, @snippet)
- page.should have_content "login function"
- page.should have_content "test.rb"
- end
- end
- end
-end
diff --git a/spec/features/snippets_spec.rb b/spec/features/snippets_spec.rb
deleted file mode 100644
index 1a0f6eaeef4..00000000000
--- a/spec/features/snippets_spec.rb
+++ /dev/null
@@ -1,99 +0,0 @@
-require 'spec_helper'
-
-describe "Snippets" do
- let(:project) { create(:project) }
-
- before do
- login_as :user
- project.team << [@user, :developer]
- end
-
- describe "GET /snippets" do
- before do
- @snippet = create(:snippet,
- author: @user,
- project: project)
-
- visit project_snippets_path(project)
- end
-
- subject { page }
-
- it { should have_content(@snippet.title[0..10]) }
- it { should have_content(@snippet.project.name) }
-
- describe "Destroy" do
- before do
- # admin access to remove snippet
- @user.users_projects.destroy_all
- project.team << [@user, :master]
- visit edit_project_snippet_path(project, @snippet)
- end
-
- it "should remove entry" do
- expect {
- click_link "destroy_snippet_#{@snippet.id}"
- }.to change { Snippet.count }.by(-1)
- end
- end
- end
-
- describe "New snippet" do
- before do
- visit project_snippets_path(project)
- click_link "New Snippet"
- end
-
- it "should open new snippet popup" do
- page.current_path.should == new_project_snippet_path(project)
- end
-
- describe "fill in", js: true do
- before do
- fill_in "snippet_title", with: "login function"
- fill_in "snippet_file_name", with: "test.rb"
- page.execute_script("editor.insert('def login; end');")
- end
-
- it { expect { click_button "Save" }.to change {Snippet.count}.by(1) }
-
- it "should add new snippet to table" do
- click_button "Save"
- page.current_path.should == project_snippet_path(project, Snippet.last)
- page.should have_content "login function"
- page.should have_content "test.rb"
- end
- end
- end
-
- describe "Edit snippet" do
- before do
- @snippet = create(:snippet,
- author: @user,
- project: project)
- visit project_snippet_path(project, @snippet)
- click_link "Edit Snippet"
- end
-
- it "should open edit page" do
- page.current_path.should == edit_project_snippet_path(project, @snippet)
- end
-
- describe "fill in" do
- before do
- fill_in "snippet_title", with: "login function"
- fill_in "snippet_file_name", with: "test.rb"
- end
-
- it { expect { click_button "Save" }.to_not change {Snippet.count} }
-
- it "should update snippet fields" do
- click_button "Save"
-
- page.current_path.should == project_snippet_path(project, @snippet)
- page.should have_content "login function"
- page.should have_content "test.rb"
- end
- end
- end
-end