diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index a1fa97914..b258ea119 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -45,6 +45,7 @@ class CropsController < ApplicationController # POST /crops # POST /crops.json def create + params[:crop][:creator_id] = current_member.id @crop = Crop.new(params[:crop]) respond_to do |format| diff --git a/app/controllers/scientific_names_controller.rb b/app/controllers/scientific_names_controller.rb index cb65fc999..202c3942c 100644 --- a/app/controllers/scientific_names_controller.rb +++ b/app/controllers/scientific_names_controller.rb @@ -45,6 +45,7 @@ class ScientificNamesController < ApplicationController # POST /scientific_names # POST /scientific_names.json def create + params[:scientific_name][:creator_id] = current_member.id @scientific_name = ScientificName.new(params[:scientific_name]) respond_to do |format| diff --git a/app/models/crop.rb b/app/models/crop.rb index c8506be8a..46eadf535 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -1,12 +1,13 @@ class Crop < ActiveRecord::Base extend FriendlyId friendly_id :system_name, use: :slugged - attr_accessible :en_wikipedia_url, :system_name, :parent_id + attr_accessible :en_wikipedia_url, :system_name, :parent_id, :creator_id has_many :scientific_names has_many :plantings has_many :photos, :through => :plantings has_many :seeds + belongs_to :creator, :class_name => 'Member' belongs_to :parent, :class_name => 'Crop' has_many :varieties, :class_name => 'Crop', :foreign_key => 'parent_id' diff --git a/app/models/scientific_name.rb b/app/models/scientific_name.rb index 919b0639e..56c86b21b 100644 --- a/app/models/scientific_name.rb +++ b/app/models/scientific_name.rb @@ -1,4 +1,5 @@ class ScientificName < ActiveRecord::Base - attr_accessible :crop_id, :scientific_name + attr_accessible :crop_id, :scientific_name, :creator_id belongs_to :crop + belongs_to :creator, :class_name => 'Member' end diff --git a/app/views/crops/edit.html.haml b/app/views/crops/edit.html.haml index 2806be189..135f74a7c 100644 --- a/app/views/crops/edit.html.haml +++ b/app/views/crops/edit.html.haml @@ -1,3 +1,10 @@ - content_for :title, "Editing crop" +%p + Added by + = @crop.creator + = distance_of_time_in_words(@crop.created_at, Time.zone.now) + ago. + = render 'form' + diff --git a/app/views/scientific_names/edit.html.haml b/app/views/scientific_names/edit.html.haml index 493ee3e44..db7f9bf71 100644 --- a/app/views/scientific_names/edit.html.haml +++ b/app/views/scientific_names/edit.html.haml @@ -1,3 +1,9 @@ - content_for :title, "Edit scientific name" +%p + Added by + = @scientific_name.creator + = distance_of_time_in_words(@scientific_name.created_at, Time.zone.now) + ago. + = render 'form' diff --git a/config/environments/development.rb b/config/environments/development.rb index 211170993..8386b8486 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -55,6 +55,7 @@ Growstuff::Application.configure do config.site_name = "Growstuff (dev)" config.analytics_code = '' config.currency = 'AUD' + config.bot_email = "noreply@growstuff.org" end config.after_initialize do diff --git a/config/environments/production.rb b/config/environments/production.rb index 08feba071..06952e9db 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -86,6 +86,7 @@ Growstuff::Application.configure do eos config.currency = 'AUD' + config.bot_email = "noreply@growstuff.org" end config.after_initialize do diff --git a/config/environments/staging.rb b/config/environments/staging.rb index 24f77f035..75cece50d 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -82,6 +82,7 @@ Growstuff::Application.configure do config.site_name = "Growstuff (staging)" config.analytics_code = '' config.currency = 'AUD' + config.bot_email = "noreply@growstuff.org" end config.after_initialize do diff --git a/config/environments/test.rb b/config/environments/test.rb index 6718e2e84..9d6affbe0 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -44,6 +44,7 @@ Growstuff::Application.configure do config.site_name = "Growstuff (test)" config.analytics_code = '' config.currency = 'AUD' + config.bot_email = "noreply@growstuff.org" end config.after_initialize do diff --git a/db/migrate/20130821011352_add_creator_to_crops.rb b/db/migrate/20130821011352_add_creator_to_crops.rb new file mode 100644 index 000000000..4da89b8d1 --- /dev/null +++ b/db/migrate/20130821011352_add_creator_to_crops.rb @@ -0,0 +1,5 @@ +class AddCreatorToCrops < ActiveRecord::Migration + def change + add_column :crops, :creator_id, :integer + end +end diff --git a/db/migrate/20130821073736_add_creator_to_scientific_name.rb b/db/migrate/20130821073736_add_creator_to_scientific_name.rb new file mode 100644 index 000000000..0905a5c40 --- /dev/null +++ b/db/migrate/20130821073736_add_creator_to_scientific_name.rb @@ -0,0 +1,5 @@ +class AddCreatorToScientificName < ActiveRecord::Migration + def change + add_column :scientific_names, :creator_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 770254e64..5a131b028 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 => 20130819004549) do +ActiveRecord::Schema.define(:version => 20130821073736) do create_table "account_types", :force => true do |t| t.string "name", :null => false @@ -58,6 +58,7 @@ ActiveRecord::Schema.define(:version => 20130819004549) do t.string "slug" t.integer "parent_id" t.integer "plantings_count" + t.integer "creator_id" end add_index "crops", ["slug"], :name => "index_crops_on_slug", :unique => true @@ -233,6 +234,7 @@ ActiveRecord::Schema.define(:version => 20130819004549) do t.integer "crop_id", :null => false t.datetime "created_at", :null => false t.datetime "updated_at", :null => false + t.integer "creator_id" end create_table "seeds", :force => true do |t| diff --git a/db/seeds.rb b/db/seeds.rb index 91ba3823a..e0f9c3792 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -12,9 +12,10 @@ require 'csv' def load_data # for all Growstuff sites, including production ones - load_crops load_roles load_basic_account_types + create_cropbot + load_crops # for development environments only if Rails.env.development? @@ -27,17 +28,18 @@ def load_data puts "Done!" end - def load_crops puts "Loading crops..." CSV.foreach(Rails.root.join('db', 'seeds', 'crops.csv')) do |row| system_name,scientific_name,en_wikipedia_url = row @crop = Crop.create( :system_name => system_name, - :en_wikipedia_url => en_wikipedia_url + :en_wikipedia_url => en_wikipedia_url, + :creator_id => @cropbot_user.id ) @crop.scientific_names.create( - :scientific_name => scientific_name + :scientific_name => scientific_name, + :creator_id => @cropbot_user.id ) end puts "Finished loading crops" @@ -102,6 +104,20 @@ def load_admin_users @wrangler_user.save! end +def create_cropbot + @cropbot_user = Member.create( + :login_name => "cropbot", + :email => Growstuff::Application.config.bot_email, + :password => SecureRandom.urlsafe_base64(64), + :tos_agreement => true + ) + @cropbot_user.confirm! + @cropbot_user.roles << @wrangler + @cropbot_user.save! + @cropbot_user.account.account_type = AccountType.find_by_name("Staff") + @cropbot_user.account.save +end + def load_paid_account_types puts "Adding 'paid' and 'seed' account types..." @paid_account = AccountType.create!( diff --git a/lib/tasks/growstuff.rake b/lib/tasks/growstuff.rake index 47a2a0416..8b9a1bada 100644 --- a/lib/tasks/growstuff.rake +++ b/lib/tasks/growstuff.rake @@ -128,9 +128,24 @@ namespace :growstuff do Crop.find_each do |c| Crop.reset_counters c.id, :plantings end - end - end + desc "August 2013: set default creator on existing crops" + task :set_default_crop_creator => :environment do + cropbot = Member.find_by_login_name("cropbot") + raise "cropbot not found: create cropbot member on site or run rake db:seed" unless cropbot + cropbot.account.account_type = AccountType.find_by_name("Staff") # set this just because it's nice + cropbot.account.save + Crop.find_each do |crop| + crop.creator = cropbot + crop.save + end + ScientificName.find_each do |sn| + sn.creator = cropbot + sn.save + end + + end + end end diff --git a/script/deploy-tasks.sh b/script/deploy-tasks.sh index 41cd8f50c..c7b85de08 100755 --- a/script/deploy-tasks.sh +++ b/script/deploy-tasks.sh @@ -8,3 +8,6 @@ echo "2013-08-18 - reset crop planting counts" rake growstuff:oneoff:reset_crop_plantings_count + +echo "2013-08-21 - set default crop creator" +rake growstuff:oneoff:set_default_crop_creator diff --git a/spec/factories/crop.rb b/spec/factories/crop.rb index 76fd31433..108aad7f8 100644 --- a/spec/factories/crop.rb +++ b/spec/factories/crop.rb @@ -3,6 +3,7 @@ FactoryGirl.define do factory :crop do system_name "Magic bean" en_wikipedia_url "http://en.wikipedia.org/wiki/Magic_bean" + creator factory :tomato do system_name "Tomato" diff --git a/spec/factories/member.rb b/spec/factories/member.rb index 5e5b7f455..c1be268b7 100644 --- a/spec/factories/member.rb +++ b/spec/factories/member.rb @@ -2,7 +2,7 @@ FactoryGirl.define do sequence(:email) { |n| "member#{n}@example.com" } sequence(:login_name) { |n| "member#{n}" } - factory :member, aliases: [:author, :owner, :sender, :recipient] do + factory :member, aliases: [:author, :owner, :sender, :recipient, :creator] do login_name { generate(:login_name) } password 'password1' email { generate(:email) } diff --git a/spec/factories/scientific_name.rb b/spec/factories/scientific_name.rb index e1bffb5cd..43d9ad74a 100644 --- a/spec/factories/scientific_name.rb +++ b/spec/factories/scientific_name.rb @@ -2,6 +2,7 @@ FactoryGirl.define do factory :scientific_name do association :crop, factory: :crop scientific_name "Beanus Magicus" + creator factory :zea_mays do association :crop, factory: :maize diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index b1363004a..55386a7b4 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -23,6 +23,11 @@ describe Crop do @crop.to_s.should == 'Tomato' "#{@crop}".should == 'Tomato' end + + it 'has a creator' do + @crop.save + @crop.creator.should be_an_instance_of Member + end end context 'invalid data' do diff --git a/spec/models/scientific_name_spec.rb b/spec/models/scientific_name_spec.rb index 7cfe62ea1..98734ec5a 100644 --- a/spec/models/scientific_name_spec.rb +++ b/spec/models/scientific_name_spec.rb @@ -16,6 +16,11 @@ describe ScientificName do @sn2 = ScientificName.find_by_scientific_name('Zea mays') @sn2.crop.system_name.should == "Maize" end + + it 'has a creator' do + @sn.save + @sn.creator.should be_an_instance_of Member + end end context 'invalid data' do diff --git a/spec/views/crops/edit.html.haml_spec.rb b/spec/views/crops/edit.html.haml_spec.rb index f8b3a4477..de5503b5b 100644 --- a/spec/views/crops/edit.html.haml_spec.rb +++ b/spec/views/crops/edit.html.haml_spec.rb @@ -9,6 +9,10 @@ describe "crops/edit" do render end + it "shows the creator" do + rendered.should contain "Added by #{@crop.creator} less than a minute ago." + end + it "renders the edit crop form" do assert_select "form", :action => crops_path(@crop), :method => "post" do assert_select "input#crop_system_name", :name => "crop[system_name]" diff --git a/spec/views/scientific_names/edit.html.haml_spec.rb b/spec/views/scientific_names/edit.html.haml_spec.rb index 30e195f52..1b48c51cd 100644 --- a/spec/views/scientific_names/edit.html.haml_spec.rb +++ b/spec/views/scientific_names/edit.html.haml_spec.rb @@ -12,6 +12,10 @@ describe "scientific_names/edit" do render end + it "shows the creator" do + rendered.should contain "Added by #{@scientific_name.creator} less than a minute ago." + end + it "renders the edit scientific_name form" do assert_select "form", :action => scientific_names_path(@scientific_name), :method => "post" do assert_select "input#scientific_name_scientific_name", :name => "scientific_name[scientific_name]"