From 87fbd50af128258e94297c0f8dd59126b206d10b Mon Sep 17 00:00:00 2001 From: Godwin Date: Mon, 26 May 2014 18:01:28 -0600 Subject: [PATCH] Changed default port and added migration tasks --- Gemfile.lock | 9 +- app/assets/stylesheets/sass/_layout.scss | 1 + app/controllers/organizations_controller.rb | 2 +- config/boot.rb | 16 +- .../20140524050931_add_about_me_to_users.rb | 5 + .../20140524052048_add_role_to_users.rb | 5 + ...d_cover_attribution_id_to_organizations.rb | 5 + ...cover_attribution_name_to_organizations.rb | 5 + ..._cover_attribution_src_to_organizations.rb | 5 + ...140525165547_add_phone_to_organizations.rb | 5 + ...organization_status_id_to_organizations.rb | 5 + ...15_add_cover_attribution_to_conferences.rb | 7 + db/schema.rb | 18 +- lib/tasks/migrate.rake | 220 ++++++++++++++++++ spec/spec_helper.rb | 6 - 15 files changed, 292 insertions(+), 22 deletions(-) create mode 100644 db/migrate/20140524050931_add_about_me_to_users.rb create mode 100644 db/migrate/20140524052048_add_role_to_users.rb create mode 100644 db/migrate/20140524202858_add_cover_attribution_id_to_organizations.rb create mode 100644 db/migrate/20140524202938_add_cover_attribution_name_to_organizations.rb create mode 100644 db/migrate/20140524203006_add_cover_attribution_src_to_organizations.rb create mode 100644 db/migrate/20140525165547_add_phone_to_organizations.rb create mode 100644 db/migrate/20140525174815_add_organization_status_id_to_organizations.rb create mode 100644 db/migrate/20140525212115_add_cover_attribution_to_conferences.rb create mode 100644 lib/tasks/migrate.rake diff --git a/Gemfile.lock b/Gemfile.lock index 5a00fda..f75cfd2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -12,6 +12,11 @@ GIT i18n-active_record (0.0.2) i18n (>= 0.5.0) +PATH + remote: ../carmen/ + specs: + carmen (1.0.1) + GEM remote: http://rubygems.org/ specs: @@ -60,8 +65,6 @@ GEM rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) - carmen (1.0.1) - unicode_utils (~> 1.4.0) carmen-rails (1.0.1) carmen (~> 1.0.0) rails @@ -283,7 +286,6 @@ GEM uglifier (2.5.0) execjs (>= 0.3.0) json (>= 1.8.0) - unicode_utils (1.4.0) wdm (0.1.0) webmock (1.18.0) addressable (>= 2.3.6) @@ -302,6 +304,7 @@ DEPENDENCIES better_errors binding_of_caller capybara + carmen! carmen-rails carrierwave carrierwave-imageoptimizer diff --git a/app/assets/stylesheets/sass/_layout.scss b/app/assets/stylesheets/sass/_layout.scss index c4ee8e5..3355ebc 100644 --- a/app/assets/stylesheets/sass/_layout.scss +++ b/app/assets/stylesheets/sass/_layout.scss @@ -221,6 +221,7 @@ $translation-control-text-color: $black !default; box-shadow: 0 0.1em 0.75em #000; position: relative; z-index: 1; + background-color: $white; } h1, h2 { diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index b697d61..5dde0b5 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -37,7 +37,7 @@ class OrganizationsController < ApplicationController params[:organization][:locations_attributes].each do |k, v| @organization.locations << Location.new(locations_organization_params(k)) end - @organization.user_organization_relationship << UserOrganizationRelationship.new(:user_id => current_user.id, :relationship => UserOrganizationRelationship::Administrator) + @organization.user_organization_relationships << UserOrganizationRelationship.new(:user_id => current_user.id, :relationship => UserOrganizationRelationship::Administrator) if @organization.save! redirect_to @organization, notice: 'Organization was successfully created.' diff --git a/config/boot.rb b/config/boot.rb index 222d337..ff4e893 100644 --- a/config/boot.rb +++ b/config/boot.rb @@ -4,12 +4,12 @@ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) require'rails/commands/server' -module Rails - class Server - alias:default_options_alias :default_options - def default_options - default_options_alias.merge!(:Port=>80) - end - end -end +#module Rails +# class Server +# alias:default_options_alias :default_options +# def default_options +# default_options_alias.merge!(:Port=>80) +# end +# end +#end diff --git a/db/migrate/20140524050931_add_about_me_to_users.rb b/db/migrate/20140524050931_add_about_me_to_users.rb new file mode 100644 index 0000000..863c4bf --- /dev/null +++ b/db/migrate/20140524050931_add_about_me_to_users.rb @@ -0,0 +1,5 @@ +class AddAboutMeToUsers < ActiveRecord::Migration + def change + add_column :users, :about_me, :text + end +end diff --git a/db/migrate/20140524052048_add_role_to_users.rb b/db/migrate/20140524052048_add_role_to_users.rb new file mode 100644 index 0000000..bac1e48 --- /dev/null +++ b/db/migrate/20140524052048_add_role_to_users.rb @@ -0,0 +1,5 @@ +class AddRoleToUsers < ActiveRecord::Migration + def change + add_column :users, :role, :string + end +end diff --git a/db/migrate/20140524202858_add_cover_attribution_id_to_organizations.rb b/db/migrate/20140524202858_add_cover_attribution_id_to_organizations.rb new file mode 100644 index 0000000..e03a82d --- /dev/null +++ b/db/migrate/20140524202858_add_cover_attribution_id_to_organizations.rb @@ -0,0 +1,5 @@ +class AddCoverAttributionIdToOrganizations < ActiveRecord::Migration + def change + add_column :organizations, :cover_attribution_id, :integer + end +end diff --git a/db/migrate/20140524202938_add_cover_attribution_name_to_organizations.rb b/db/migrate/20140524202938_add_cover_attribution_name_to_organizations.rb new file mode 100644 index 0000000..c279821 --- /dev/null +++ b/db/migrate/20140524202938_add_cover_attribution_name_to_organizations.rb @@ -0,0 +1,5 @@ +class AddCoverAttributionNameToOrganizations < ActiveRecord::Migration + def change + add_column :organizations, :cover_attribution_name, :string + end +end diff --git a/db/migrate/20140524203006_add_cover_attribution_src_to_organizations.rb b/db/migrate/20140524203006_add_cover_attribution_src_to_organizations.rb new file mode 100644 index 0000000..2365fd1 --- /dev/null +++ b/db/migrate/20140524203006_add_cover_attribution_src_to_organizations.rb @@ -0,0 +1,5 @@ +class AddCoverAttributionSrcToOrganizations < ActiveRecord::Migration + def change + add_column :organizations, :cover_attribution_src, :string + end +end diff --git a/db/migrate/20140525165547_add_phone_to_organizations.rb b/db/migrate/20140525165547_add_phone_to_organizations.rb new file mode 100644 index 0000000..5c85c1f --- /dev/null +++ b/db/migrate/20140525165547_add_phone_to_organizations.rb @@ -0,0 +1,5 @@ +class AddPhoneToOrganizations < ActiveRecord::Migration + def change + add_column :organizations, :phone, :string + end +end diff --git a/db/migrate/20140525174815_add_organization_status_id_to_organizations.rb b/db/migrate/20140525174815_add_organization_status_id_to_organizations.rb new file mode 100644 index 0000000..fcb26ca --- /dev/null +++ b/db/migrate/20140525174815_add_organization_status_id_to_organizations.rb @@ -0,0 +1,5 @@ +class AddOrganizationStatusIdToOrganizations < ActiveRecord::Migration + def change + add_column :organizations, :organization_status_id, :integer + end +end diff --git a/db/migrate/20140525212115_add_cover_attribution_to_conferences.rb b/db/migrate/20140525212115_add_cover_attribution_to_conferences.rb new file mode 100644 index 0000000..b65439c --- /dev/null +++ b/db/migrate/20140525212115_add_cover_attribution_to_conferences.rb @@ -0,0 +1,7 @@ +class AddCoverAttributionToConferences < ActiveRecord::Migration + def change + add_column :conferences, :cover_attribution_id, :integer + add_column :conferences, :cover_attribution_name, :string + add_column :conferences, :cover_attribution_src, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index feab20a..edc79fe 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140315183241) do +ActiveRecord::Schema.define(version: 20140525212115) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -90,6 +90,9 @@ ActiveRecord::Schema.define(version: 20140315183241) do t.text "preregistration_info" t.text "registration_info" t.text "postregistration_info" + t.integer "cover_attribution_id" + t.string "cover_attribution_name" + t.string "cover_attribution_src" end create_table "event_types", force: true do |t| @@ -158,6 +161,11 @@ ActiveRecord::Schema.define(version: 20140315183241) do t.datetime "created_at" t.datetime "updated_at" t.string "cover" + t.integer "cover_attribution_id" + t.string "cover_attribution_name" + t.string "cover_attribution_src" + t.string "phone" + t.integer "organization_status_id" end create_table "registration_form_fields", force: true do |t| @@ -182,8 +190,8 @@ ActiveRecord::Schema.define(version: 20140315183241) do end create_table "user_organization_relationships", force: true do |t| - t.integer "user_id" - t.integer "organization_id" + t.integer "user_id", null: false + t.integer "organization_id", null: false t.string "relationship" t.datetime "created_at" t.datetime "updated_at" @@ -208,6 +216,8 @@ ActiveRecord::Schema.define(version: 20140315183241) do t.datetime "lock_expires_at" t.string "unlock_token" t.string "avatar" + t.text "about_me" + t.string "role" end add_index "users", ["activation_token"], name: "index_users_on_activation_token", using: :btree @@ -221,7 +231,7 @@ ActiveRecord::Schema.define(version: 20140315183241) do t.string "whodunnit" t.text "object" t.datetime "created_at" - t.string "value" + t.text "value" end create_table "workshop_facilitators", force: true do |t| diff --git a/lib/tasks/migrate.rake b/lib/tasks/migrate.rake new file mode 100644 index 0000000..58746d7 --- /dev/null +++ b/lib/tasks/migrate.rake @@ -0,0 +1,220 @@ +require 'open-uri' +require 'json' +require 'rest_client' +require 'geocoder/calculations' + +namespace :migrate do + desc "Migrates data from live site to current database" + + task users: :environment do + migrate! User.new + end + + task organization_statuses: :environment do + migrate!(OrganizationStatus.new, true, 'shop_status') + OrganizationStatus.create({:title => 'Unknown', :slug => 'unknown'}) + end + + task organizations: :environment do + clear_table 'locations_organizations' + clear_table 'user_organization_relationships' + migrate! Organization.new + end + + task conference_types: :environment do + clear_table 'conference_types' + ConferenceType.create({:id => 4, :title => 'Official Bike!Bike!', :slug => 'bikebike'}) + ConferenceType.create({:id => 5, :title => 'Regional Bike!Bike!', :slug => 'regional'}) + ConferenceType.create({:id => 6, :title => 'BiciCongreso', :slug => 'bici-congreso'}) + end + + task conferences: :environment do + migrate! Conference.new + end + + task workshops: :environment do + migrate! Workshop.new + end + + task conferences: :environment do + clear_table 'conference_admins' + clear_table 'conference_host_organizations' + migrate! Conference.new + end + + def migrate_user(id, object) + if id.to_i > 0 + object.symbolize_keys! + params = + { + :id => id, + :username => object[:name], + :email => object[:mail], + :crypted_password => object[:pass], + :created_at => Time.at(object[:created].to_i).to_datetime, + :role => object[:roles].to_a.last.last.split(' ').last + } + return get_image(object[:picture], :avatar, params) + end + return nil + end + + def migrate_organization(id, object) + location = object[:field_location]['und'].first + location.symbolize_keys! + params = + { + :id => id, + :name => object[:title], + :slug => object[:path].gsub(/^.*\/(.*)$/, '\1'), + :email_address => object[:email], + :url => object[:field_website] && object[:field_website].first ? object[:field_website]['und'][0]['url'] : nil, + :year_founded => object[:field_year_founded] && object[:field_year_founded].first ? object[:field_year_founded]['und'][0]['value'] : nil, + :info => object[:body]['und'][0]['value'], + :organization_status_id => object[:field_shop_status] && object[:field_shop_status].first ? object[:field_shop_status]['und'][0]['tid'] : nil, + :phone => location[:phone], + :created_at => Time.at(object[:created].to_i).to_datetime + } + logo = object[:field_logo] && object[:field_logo].first ? object[:field_logo]['und'].first : object[:field_icon]['und'].first + params = get_image(logo, :avatar, params) + params = get_panoramio_image(location[:city], location[:province], location[:country], :cover, params) + return params + end + + def organization_post_save(json, object) + l = json[:field_location]['und'].first + l.symbolize_keys! + lparams = { + :id => l[:lid], + :title => l[:name], + :latitude => l[:latitude], + :longitude => l[:longitude], + :longitude => l[:longitude], + :country => l[:country].upcase, + :territory => l[:province].upcase, + :city => l[:city], + :street => l[:street], + :postal_code => l[:postal_code] + } + location = nil + begin + location = Location.create(lparams) + rescue + location = Location.find(l[:lid]) + location.update_attributes(lparams) + end + + object.locations << location + json[:field_administrators]['und'].each { |u| + object.user_organization_relationships << UserOrganizationRelationship.new(:user_id => u['target_id'].to_i, :relationship => UserOrganizationRelationship::Administrator) + } + object.save! + end + + def migrate_conference(id, object) + host = Organization.find(object[:field_host_organizations]['und'].first['target_id'].to_i) + location = host.locations.first + params = + { + :id => id, + :title => object[:title], + :slug => object[:path].gsub(/^.*\/(.*)$/, '\1'), + :conference_type_id => object[:field_conference_type] && object[:field_conference_type].first ? object[:field_conference_type]['und'][0]['tid'] : nil, + :info => object[:body]['und'][0]['value'], + :start_date => object[:field_date]['und'][0]['value'], + :end_date => object[:field_date]['und'][0]['value2'], + :workshop_schedule_published => object[:field_workshops_published]['und'][0]['value'].to_i, + :registration_open => object[:field_registration_open]['und'][0]['value'].to_i, + :meals_provided => object[:field_meals_provided]['und'][0]['value'].to_i, + :meal_info => object[:field_meal_info] && object[:field_meal_info].first ? object[:field_meal_info]['und'][0]['value'] : '', + :created_at => Time.at(object[:created].to_i).to_datetime + } + params = get_image(object[:field_banner]['und'].first, :poster, params) + params = get_panoramio_image(location.city, location.territory, location.country, :cover, params) + return params + end + + def conference_post_save(json, object) + object.locations << location + i = 0 + json[:field_host_organizations]['und'].each { |u| + object.conference_host_organizations << ConferenceHostOrganization.new(:organization_id => u['target_id'].to_i, :order => i) + i += 1 + } + object.save! + end + + def migrate_organization_status(id, object) + return { + :id => id, + :name => object[:name], + :slug => object[:name].split(/\s/).first.downcase + } + end + + def get_image(picture, column, params) + if picture + params["remote_#{column.to_s}_url".to_sym] = 'https://www.bikebike.org/sites/default/files/' + picture['uri'].gsub(/^public:\/\/(.*)$/, '\1') + params[column.to_sym] = picture['filename'] + end + return params + end + + def get_panoramio_image(city, territory, country, column, params) + result = Geocoder.search(city + ', ' + (territory ? territory + ' ' : '') + country).first + if result + points = Geocoder::Calculations.bounding_box([result.latitude, result.longitude], 5, { :unit => :km }) + response = RestClient.get 'http://www.panoramio.com/map/get_panoramas.php', :params => {:set => :public, :size => :original, :from => 0, :to => 20, :mapfilter => false, :miny => points[0], :minx => points[1], :maxy => points[2], :maxx => points[3]} + if response.code == 200 + JSON.parse(response.to_str)['photos'].each { |img| + if img['width'].to_i > 980 + params["remote_#{column.to_s}_url".to_sym] = img['photo_file_url'] + params[column.to_sym] = img['photo_file_url'].gsub(/^.*\/(.*)$/, '\1') + params[:cover_attribution_id] = img['owner_id'] + params[:cover_attribution_name] = img['owner_name'] + params[:cover_attribution_src] = 'panoramio' + return params + end + } + end + end + return params + end + + def migrate!(model, vocabulary = false, table = nil) + type = model.class.table_name + clear_table(type) + is_users = (type == 'users') + table ||= type.singularize + get_data(is_users ? 'user' : (vocabulary ? 'taxonomy_term' : 'node'), is_users || vocabulary ? nil : table, vocabulary ? table : nil).each { |id, object| + object.symbolize_keys! + params = self.send('migrate_' + type.singularize, id, object) + if params + new_object = model.class.create(params) + post_save = (type.singularize + '_post_save') + begin + self.send(post_save, object, new_object) + rescue; end + end + } + end + + def clear_table(table) + ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table} RESTART IDENTITY;") + end + + def get_data(entity_type, node_type = nil, vocabulary = nil) + params = "dunhooser=#{entity_type}" + if node_type + params += "&actakinding=#{node_type}" + elsif vocabulary + params += "&folloo=#{vocabulary}" + end + url = "https://www.bikebike.org/reformulmatics?#{params}" + begin + JSON.parse(open(url).read) + rescue + puts url + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8f5325b..b69aa9b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -58,10 +58,4 @@ RSpec.configure do |config| #visit 1, 2, 3 end - def visit(*args) - puts "\n\n========= VISIT!!! =========\n\n" - #super(page) - session.visit(*args) - end - end