diff --git a/Gemfile.lock b/Gemfile.lock index 2236912c..a9b07fba 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -117,7 +117,7 @@ GEM hashie (2.0.3) hike (1.2.3) httpauth (0.2.0) - i18n (0.6.11) + i18n (0.7.0) inherited_resources (1.4.0) has_scope (~> 0.5.0) responders (~> 0.9) @@ -141,7 +141,7 @@ GEM polyamorous (~> 0.5.0) mime-types (1.25.1) mini_portile (0.5.2) - multi_json (1.10.1) + multi_json (1.11.0) multipart-post (1.2.0) newrelic_rpm (3.9.0.229) nokogiri (1.6.1) @@ -222,7 +222,7 @@ GEM sawyer (0.5.5) addressable (~> 2.3.5) faraday (~> 0.8, < 0.10) - shoulda-matchers (2.0.0) + shoulda-matchers (2.8.0) activesupport (>= 3.0.0) simple_form (2.1.0) actionpack (~> 3.0) diff --git a/app/models/organization.rb b/app/models/organization.rb index 72891fcb..cb9d2777 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -1,19 +1,23 @@ class Organization < ActiveRecord::Base before_save :delete_logo + before_create :set_github_org has_many :jobs has_many :projects has_many :organization_metrics has_many :sponsorships - attr_accessible :name, :url, :github_org, :description, :is_tax_exempt, :contact_name, :contact_role, :contact_email, :annual_budget_usd, :total_staff_size, :tech_staff_size, :notes, :image_url, :twitter, :logo, :logo_delete + attr_accessible :name, :url, :github_org, :description, + :is_tax_exempt, :contact_name, :contact_role, + :contact_email, :annual_budget_usd, :total_staff_size, + :tech_staff_size, :notes, :image_url, :twitter, :logo, + :logo_delete attr_accessible :organization_metrics_attributes, :projects_attributes attr_writer :logo_delete attr_accessor :is_public_submission - validates_presence_of :name - validates_presence_of :github_org, if: :is_public_submission + validates :name, presence: true # Paperclip has_attached_file :logo, styles: { thumb: '100x100>', medium: '250x250>' }, @@ -48,4 +52,12 @@ def logo_delete def delete_logo logo.clear if logo_delete == '1' end + + def set_github_org + if projects.first + self.github_org = Project.parse_git_url(projects.first.submitted_github_url) + .split('/') + .first + end + end end diff --git a/app/models/project.rb b/app/models/project.rb index a1b111ea..d4b66230 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,12 +1,21 @@ class Project < ActiveRecord::Base + before_create :set_github_repo belongs_to :organization acts_as_ordered_taggable acts_as_ordered_taggable_on :technologies, :causes attr_accessible :cause_list, :description, :github_repo, :help_url, :install_url, :is_active, :is_approved, :name, :notes, - :organization_id, :technology_list, :url, :twitter - validates_presence_of :name, :github_repo + :organization_id, :technology_list, :url, :twitter, + :submitted_github_url + + attr_accessor :submitted_github_url + validates :name, presence: true + validates :github_repo, presence: true, on: :update + validates :submitted_github_url, presence: true, + format: { with: /\Ahttps?:\/\/github.com\/[\w-]+\/[\w\.-]+(\/)?\Z/i, + message: 'Please enter a valid GitHub URL.' }, + on: :create has_many :favorite_projects has_many :users, through: :favorite_projects @@ -60,4 +69,15 @@ def related_projects def tasks_url "#{github_url}/issues" end + + def self.parse_git_url(url) + url.gsub(/^(((https|http|git)?:\/\/(www\.)?)|git@)github.com(:|\/)/i, '') + .gsub(/(\.git|\/)$/i, '') + end + + private + + def set_github_repo + self.github_repo = Project.parse_git_url(submitted_github_url).split('/').last + end end diff --git a/app/views/organizations/new.html.erb b/app/views/organizations/new.html.erb index 6c19fa01..5d9e0a54 100644 --- a/app/views/organizations/new.html.erb +++ b/app/views/organizations/new.html.erb @@ -7,15 +7,14 @@ <%= f.simple_fields_for :projects do |project| %>
<%= project.input :name, :label => 'Project Name', wrapper_html: { class: "large-6 columns" }, error_html: { class: "error"} %> - <%= project.input :github_repo, :label => 'GitHub Repository', :placeholder => 'Just the repo name, please...', wrapper_html: { class: 'large-6 columns' }, error_html: { class: "error"} %> + <%= project.input :submitted_github_url, :label => 'GitHub Repository URL', :placeholder => 'Just the repo url, please...', wrapper_html: { class: 'large-6 columns' }, error_html: { class: "error"} %>
<%= project.input :url, :label => 'Project Website' %> <%= project.input :description, :placeholder => 'What does your project do?' %> <% end %> - <%= f.input :name, :label => 'Organization Name', error_html: { class: "error" } %>
- <%= f.input :github_org, :label => 'Organization GitHub Username', :required => true, wrapper_html: { class: 'large-6 columns'}, error_html: { class: "error"} %> + <%= f.input :name, :label => 'Organization Name', wrapper_html: { class: 'large-6 columns'}, error_html: { class: "error" } %> <%= f.input :url, :label => 'Organization Website', :required => false, :as => :url, wrapper_html: { class: 'large-6 columns'} %>
@@ -32,4 +31,4 @@

Already a CodeMontage Organization? <%= mail_to "projects@codemontage.com", "Submit a New Project", :subject => "New project for my organization" %>

- + \ No newline at end of file diff --git a/spec/factories/projects.rb b/spec/factories/projects.rb index 694add8f..5abab6c5 100644 --- a/spec/factories/projects.rb +++ b/spec/factories/projects.rb @@ -3,5 +3,6 @@ association :organization name "CodeMontage" github_repo "codemontage" + submitted_github_url "https://github.com/CodeMontageHQ/codemontage" end end diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb index 82f71a05..05679831 100644 --- a/spec/models/organization_spec.rb +++ b/spec/models/organization_spec.rb @@ -6,11 +6,6 @@ it { should validate_presence_of(:name) } context 'validations' do - context 'if public submission' do - before { subject.stub(:is_public_submission) { true } } - it { should validate_presence_of(:github_org) } - end - context 'if not public submission' do before { subject.stub(:is_public_submission) { false } } it { should_not validate_presence_of(:github_org) } @@ -21,6 +16,24 @@ end end + context 'create organization' do + let(:organization) { + Organization.new(projects_attributes: { "0" => + { + name: "sidekiq", + submitted_github_url: "https://github.com/mperham/sidekiq" + } + }, + name: "Mperham") + } + + it 'populates github_repo and github_org upon create' do + organization.save + expect(organization.projects.first.github_repo).to eq('sidekiq') + expect(organization.github_org).to eq('mperham') + end + end + context 'url wrangling' do let(:organization) { Organization.new(url: 'http://www.amazing.org', github_org: 'amazing') } diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index a17254ef..d8cddf83 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -6,7 +6,8 @@ it { should have_many(:events).through(:featured_projects) } it { should validate_presence_of(:name) } - it { should validate_presence_of(:github_repo) } + it { should validate_presence_of(:submitted_github_url).on(:create) } + it { should validate_presence_of(:github_repo).on(:update) } context 'github-related methods' do let(:project) { Project.new } @@ -86,12 +87,18 @@ let(:organization) { Organization.create!(name: 'CodeMontage') } before do - @project_1 = Project.create!(name: 'Code Montage', organization_id: organization.id, github_repo: 'codemontage') - @project_2 = Project.create!(name: 'Happy Days', organization_id: organization.id, github_repo: 'happydays') + @project_1 = Project.create!(name: 'Code Montage', organization_id: organization.id, submitted_github_url: 'https://github.com/CodeMontageHQ/codemontage') + @project_2 = Project.create!(name: 'Happy Days', organization_id: organization.id, submitted_github_url: 'https://github.com/kig/happydays') end it "returns its organization's other projects" do expect(@project_1.related_projects).to eq([@project_2]) end end + + describe '.parse_git_url' do + it "returns organization/repo given a github project url" do + expect(Project.parse_git_url('https://github.com/CodeMontageHQ/codemontage')).to eq('CodeMontageHQ/codemontage') + end + end end