From 6c471c50b1205c6504742355c224fdb8ee2c8498 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Wed, 12 Feb 2014 00:23:34 +0400 Subject: [PATCH] #349: added #automatic_metadata_regeneration to Platform --- ...omatic_metadata_regeneration_controller.js | 17 +++++++++++ .../javascripts/angularjs/locales.js.erb | 4 +++ .../platforms/platforms_controller.rb | 28 +++++++++++++------ app/models/platform.rb | 4 ++- app/views/platforms/platforms/edit.html.haml | 10 +++++++ config/locales/models/platform.en.yml | 1 + config/locales/models/platform.ru.yml | 1 + ...matic_metadata_regeneration_to_platform.rb | 5 ++++ db/schema.rb | 13 +++++---- spec/models/platform_spec.rb | 6 ++++ 10 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 app/assets/javascripts/angularjs/controllers/automatic_metadata_regeneration_controller.js create mode 100644 db/migrate/20140211175858_add_automatic_metadata_regeneration_to_platform.rb diff --git a/app/assets/javascripts/angularjs/controllers/automatic_metadata_regeneration_controller.js b/app/assets/javascripts/angularjs/controllers/automatic_metadata_regeneration_controller.js new file mode 100644 index 000000000..a05b86078 --- /dev/null +++ b/app/assets/javascripts/angularjs/controllers/automatic_metadata_regeneration_controller.js @@ -0,0 +1,17 @@ +RosaABF.controller('AutomaticMetadataRegenerationController', ['$scope', '$http', function($scope, $http) { + + // See: Platfrom::AUTOMATIC_METADATA_REGENERATIONS + $scope.items = { + 'day': 'platform.automatic_metadata_regeneration.day', + 'week': 'platform.automatic_metadata_regeneration.week' + }; + $scope.platform_id = null; + + $scope.update = function() { + $http.put( + Routes.platform_path($scope.platform_id), + {platform: {automatic_metadata_regeneration: $scope.amr}, format: 'json'} + ); + } + +}]); \ No newline at end of file diff --git a/app/assets/javascripts/angularjs/locales.js.erb b/app/assets/javascripts/angularjs/locales.js.erb index fdd2c2d0c..92b133047 100644 --- a/app/assets/javascripts/angularjs/locales.js.erb +++ b/app/assets/javascripts/angularjs/locales.js.erb @@ -11,6 +11,8 @@ var _locales = { 'Всего %1 тега', 'Всего %1 тегов' ], + 'platform.automatic_metadata_regeneration.day': 'Раз в день', + 'platform.automatic_metadata_regeneration.week': 'Раз в неделю', <%= BuildList::STATUSES.map{|s| "'build_list.status.#{s}': '#{BuildList.human_status(s)}'"}.join(',') %> }, <%I18n.locale = :en%> @@ -23,6 +25,8 @@ var _locales = { 'Total %1 tag', 'Total %1 tags' ], + 'platform.automatic_metadata_regeneration.day': 'Once a day', + 'platform.automatic_metadata_regeneration.week': 'Once a week', <%= BuildList::STATUSES.map{|s| "'build_list.status.#{s}': '#{BuildList.human_status(s)}'"}.join(',') %> } }; \ No newline at end of file diff --git a/app/controllers/platforms/platforms_controller.rb b/app/controllers/platforms/platforms_controller.rb index 2efcf0b74..f1c094fef 100644 --- a/app/controllers/platforms/platforms_controller.rb +++ b/app/controllers/platforms/platforms_controller.rb @@ -43,16 +43,28 @@ class Platforms::PlatformsController < Platforms::BaseController @admin_uname = params[:admin_uname] platform_params = params[:platform] || {} - platform_params = platform_params.slice(:description, :platform_arch_settings_attributes, :released) + platform_params = platform_params.slice(:description, :platform_arch_settings_attributes, :released, :automatic_metadata_regeneration) platform_params[:owner] = User.find(@admin_id) if @admin_id.present? - if @platform.update_attributes(platform_params) - flash[:notice] = I18n.t("flash.platform.saved") - redirect_to @platform - else - flash[:error] = I18n.t("flash.platform.save_error") - flash[:warning] = @platform.errors.full_messages.join('. ') - render action: :edit + + respond_to do |format| + format.html do + if @platform.update_attributes(platform_params) + flash[:notice] = I18n.t("flash.platform.saved") + redirect_to @platform + else + flash[:error] = I18n.t("flash.platform.save_error") + flash[:warning] = @platform.errors.full_messages.join('. ') + render action: :edit + end + end + format.json do + if @platform.update_attributes(platform_params) + render json: { notice: I18n.t("flash.platform.saved") }.to_json + else + render json: { error: I18n.t("flash.platform.save_error") }.to_json, status: 433 + end + end end end diff --git a/app/models/platform.rb b/app/models/platform.rb index a6dc61fcb..a830d2644 100644 --- a/app/models/platform.rb +++ b/app/models/platform.rb @@ -5,6 +5,7 @@ class Platform < ActiveRecord::Base include Modules::Models::FileStoreClean include Modules::Models::RegenerationStatus + AUTOMATIC_METADATA_REGENERATIONS = %w(day week) VISIBILITIES = %w(open hidden) NAME_PATTERN = /[\w\-\.]+/ HUMAN_STATUSES = HUMAN_STATUSES.clone.freeze @@ -30,6 +31,7 @@ class Platform < ActiveRecord::Base validates :description, presence: true validates :visibility, presence: true, inclusion: {in: VISIBILITIES} + validates :automatic_metadata_regeneration, inclusion: {in: AUTOMATIC_METADATA_REGENERATIONS}, allow_blank: true validates :name, uniqueness: {case_sensitive: false}, presence: true, format: { with: /\A#{NAME_PATTERN}\z/ } validates :distrib_type, presence: true, inclusion: {in: APP_CONFIG['distr_types']} validate lambda { @@ -63,7 +65,7 @@ class Platform < ActiveRecord::Base scope :waiting_for_regeneration, where(status: WAITING_FOR_REGENERATION) accepts_nested_attributes_for :platform_arch_settings, allow_destroy: true - attr_accessible :name, :distrib_type, :parent_platform_id, :platform_type, :owner, :visibility, :description, :released, :platform_arch_settings_attributes + attr_accessible :name, :distrib_type, :parent_platform_id, :platform_type, :owner, :visibility, :description, :released, :platform_arch_settings_attributes, :automatic_metadata_regeneration attr_readonly :name, :distrib_type, :parent_platform_id, :platform_type include Modules::Models::Owner diff --git a/app/views/platforms/platforms/edit.html.haml b/app/views/platforms/platforms/edit.html.haml index 5e90fd195..2e12dedd2 100644 --- a/app/views/platforms/platforms/edit.html.haml +++ b/app/views/platforms/platforms/edit.html.haml @@ -21,15 +21,25 @@ .leftlist= t('activerecord.attributes.regeneration_status.status') .rightlist= t("layout.regeneration_statuses.statuses.#{@platform.human_status}") .both + .leftlist= t('activerecord.attributes.regeneration_status.last_regenerated_at') .rightlist= @platform.last_regenerated_at .both + .leftlist= t('activerecord.attributes.regeneration_status.last_regenerated_status') .rightlist= t("layout.regeneration_statuses.last_regenerated_statuses.#{@platform.human_regeneration_status}") .both + .leftlist= t('activerecord.attributes.regeneration_status.last_regenerated_log_sha1') .rightlist= link_to_file_store('regeneration.log', @platform.last_regenerated_log_sha1) .both + + .leftlist= t('activerecord.attributes.platform.automatic_metadata_regeneration') + .rightlist{ 'ng-controller' => 'AutomaticMetadataRegenerationController', 'ng-init' => "platform_id = #{@platform.id}" } + %select{ 'ng-options' => 'k as (v | i18n) for (k, v) in items', 'ng-model' => 'amr', 'ng-change' => 'update()', 'ng-init' => "amr = '#{@platform.automatic_metadata_regeneration.to_s}'" } + %option{ value: '' } + .both + .leftside .rightside= link_to t('layout.regeneration_statuses.regenerate_metadata'), regenerate_metadata_platform_path(@platform), method: :put, confirm: t('layout.confirm'), class: 'button' .both diff --git a/config/locales/models/platform.en.yml b/config/locales/models/platform.en.yml index d4722ed81..e59d5bc29 100644 --- a/config/locales/models/platform.en.yml +++ b/config/locales/models/platform.en.yml @@ -92,6 +92,7 @@ en: updated_at: Updated distrib_type: Source type visibility: Status + automatic_metadata_regeneration: Automatic metadata regeneration visibility_types: open: Public hidden: Private diff --git a/config/locales/models/platform.ru.yml b/config/locales/models/platform.ru.yml index 7b8db4de3..cea910b36 100644 --- a/config/locales/models/platform.ru.yml +++ b/config/locales/models/platform.ru.yml @@ -92,6 +92,7 @@ ru: updated_at: Обновлена distrib_type: Тип дистрибутива visibility: Статус + automatic_metadata_regeneration: Автоматическая регенерация метаданных visibility_types: open: Публичный hidden: Приватный diff --git a/db/migrate/20140211175858_add_automatic_metadata_regeneration_to_platform.rb b/db/migrate/20140211175858_add_automatic_metadata_regeneration_to_platform.rb new file mode 100644 index 000000000..815952267 --- /dev/null +++ b/db/migrate/20140211175858_add_automatic_metadata_regeneration_to_platform.rb @@ -0,0 +1,5 @@ +class AddAutomaticMetadataRegenerationToPlatform < ActiveRecord::Migration + def change + add_column :platforms, :automatic_metadata_regeneration, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index eee0323ca..92c3bfa6f 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 => 20140113215223) do +ActiveRecord::Schema.define(:version => 20140211175858) do create_table "activity_feeds", :force => true do |t| t.integer "user_id", :null => false @@ -316,20 +316,21 @@ ActiveRecord::Schema.define(:version => 20140113215223) do create_table "platforms", :force => true do |t| t.string "description" - t.string "name", :null => false + t.string "name", :null => false t.integer "parent_platform_id" t.datetime "created_at" t.datetime "updated_at" - t.boolean "released", :default => false, :null => false + t.boolean "released", :default => false, :null => false t.integer "owner_id" t.string "owner_type" - t.string "visibility", :default => "open", :null => false - t.string "platform_type", :default => "main", :null => false - t.string "distrib_type", :null => false + t.string "visibility", :default => "open", :null => false + t.string "platform_type", :default => "main", :null => false + t.string "distrib_type", :null => false t.integer "status" t.datetime "last_regenerated_at" t.integer "last_regenerated_status" t.string "last_regenerated_log_sha1" + t.string "automatic_metadata_regeneration" end add_index "platforms", ["name"], :name => "index_platforms_on_name", :unique => true, :case_sensitive => false diff --git a/spec/models/platform_spec.rb b/spec/models/platform_spec.rb index e65e6774a..7b176775c 100644 --- a/spec/models/platform_spec.rb +++ b/spec/models/platform_spec.rb @@ -27,6 +27,12 @@ describe Platform do end it {should_not allow_value('custom_status').for(:visibility)} + ['', nil] + Platform::AUTOMATIC_METADATA_REGENERATIONS.each do |value| + it {should allow_value(value).for(:automatic_metadata_regeneration)} + end + it {should_not allow_value('custom_status').for(:visibility)} + + it { should have_readonly_attribute(:name) } it { should have_readonly_attribute(:distrib_type) } it { should have_readonly_attribute(:parent_platform_id) }