diff --git a/Gemfile b/Gemfile index d598ac1dd0..6701685db6 100644 --- a/Gemfile +++ b/Gemfile @@ -72,5 +72,4 @@ group :development, :test do gem 'devise' gem 'paperclip', '~> 2.7' end - gemspec diff --git a/app/controllers/rails_admin/main_controller.rb b/app/controllers/rails_admin/main_controller.rb index 684a16d7cb..2cf036f8af 100644 --- a/app/controllers/rails_admin/main_controller.rb +++ b/app/controllers/rails_admin/main_controller.rb @@ -10,7 +10,6 @@ class MainController < RailsAdmin::ApplicationController include RailsAdmin::ApplicationHelper include Maintenance::Tags - layout :get_layout before_filter :get_model, :except => RailsAdmin::Config::Actions.all(:root).map(&:action_name) @@ -43,7 +42,6 @@ def bulk_action self.send(params[:bulk_action]) if params[:bulk_action].in?(RailsAdmin::Config::Actions.all(:controller => self, :abstract_model => @abstract_model).select(&:bulkable?).map(&:route_fragment)) end - def persist_objects @filtered_objs = @objects if params.has_key?(:f) @@ -60,27 +58,27 @@ def list_entries(is_edit, model_config = @model_config, auth_scope_key = :index, @entries = get_collection(is_edit, model_config, scope, pagination) @entries end - - private - - def get_layout - "rails_admin/#{request.headers['X-PJAX'] ? 'pjax' : 'application'}" - end - def back_or_index - params[:return_to].presence && params[:return_to].include?(request.host) && (params[:return_to] != request.fullpath) ? params[:return_to] : index_path - end + private + def get_layout + "rails_admin/#{request.headers['X-PJAX'] ? 'pjax' : 'application'}" + end - def get_sort_hash(model_config) - abstract_model = model_config.abstract_model - params[:sort] = params[:sort_reverse] = nil unless model_config.list.fields.map {|f| f.name.to_s}.include? params[:sort] + def back_or_index + params[:return_to].presence && params[:return_to].include?(request.host) && (params[:return_to] != request.fullpath) ? params[:return_to] : index_path + end - params[:sort] ||= model_config.list.sort_by.to_s - params[:sort_reverse] ||= 'false' - field = model_config.list.fields.find{ |f| f.name.to_s == params[:sort] } + def get_sort_hash(model_config) + abstract_model = model_config.abstract_model + params[:sort] = params[:sort_reverse] = nil unless model_config.list.fields.map {|f| f.name.to_s}.include? params[:sort] + + params[:sort] ||= model_config.list.sort_by.to_s + params[:sort_reverse] ||= 'false' + + field = model_config.list.fields.find{ |f| f.name.to_s == params[:sort] } column = if field.nil? || field.sortable == true # use params[:sort] on the base table "#{abstract_model.table_name}.#{params[:sort]}" @@ -116,7 +114,7 @@ def redirect_to_on_success obj = @objects[session["index"]] @object.resolve true redirect_to edit_path(:id => obj.id, :return_to => params[:return_to]), :flash => { :success => notice } - elsif params[:_invalid_another] + elsif params[:_invalid_another] @objects ||= list_entries(true) obj = @objects[session["index"]] @object.resolve false @@ -171,12 +169,12 @@ def check_for_cancel def get_collection(is_edit, model_config, scope, pagination) or_filters = Hash.new if params[:f] != nil - params[:f].each do |param| - puts param[0] - or_filters[param[0]] = param[1] if param[0].start_with? "OR" - params[:f].delete param[0] if param[0].start_with? "OR" + params[:f].each do |param| + puts param[0] + or_filters[param[0]] = param[1] if param[0].start_with? "OR" + params[:f].delete param[0] if param[0].start_with? "OR" + end end - end puts or_filters if params.has_key?(:object_type) if params[:object_type] != "All" @@ -240,6 +238,29 @@ def get_collection(is_edit, model_config, scope, pagination) end end + + + if params.has_key?(:category_cache) + if params[:category_cache] != "All" + search_query = Hash.new + search_query[:o] = "is" + search_query[:v] = params[:category_cache] + query_hash = Hash.new + query_hash["94742"] = search_query + if params[:f] != nil + params[:f][:category_cache] = query_hash + else + params[:f] = Hash.new + params[:f][:category_cache] = query_hash + end + else + if params[:f] != nil + params[:f].delete(:category_cache) + end + + end + end + puts query_hash puts params if params.has_key?(:f) @@ -271,31 +292,37 @@ def get_collection(is_edit, model_config, scope, pagination) params[:f].delete(:reason) if params[:f] != nil params[:f].delete(:status) if params[:f] != nil # params[:f].merge(:or_filters) if :or_filters.present? - return objects - end + return objects + end - - def get_association_scope_from_params - return nil unless params[:associated_collection].present? - source_abstract_model = RailsAdmin::AbstractModel.new(to_model_name(params[:source_abstract_model])) - source_model_config = source_abstract_model.config - source_object = source_abstract_model.get(params[:source_object_id]) - action = params[:current_action].in?(['create', 'update']) ? params[:current_action] : 'edit' - @association = source_model_config.send(action).fields.find{|f| f.name == params[:associated_collection].to_sym }.with(:controller => self, :object => source_object) - @association.associated_collection_scope - end - def associations_hash - associations = {} - @abstract_model.associations.each do |association| - if [:has_many, :has_and_belongs_to_many].include?(association[:type]) - records = Array(@object.send(association[:name])) - associations[association[:name]] = records.collect(&:id) - end + def get_association_scope_from_params + return nil unless params[:associated_collection].present? + source_abstract_model = RailsAdmin::AbstractModel.new(to_model_name(params[:source_abstract_model])) + source_model_config = source_abstract_model.config + source_object = source_abstract_model.get(params[:source_object_id]) + action = params[:current_action].in?(['create', 'update']) ? params[:current_action] : 'edit' + @association = source_model_config.send(action).fields.find{|f| f.name == params[:associated_collection].to_sym }.with(:controller => self, :object => source_object) + @association.associated_collection_scope + end + + def associations_hash + associations = {} + @abstract_model.associations.each do |association| + if [:has_many, :has_and_belongs_to_many].include?(association[:type]) + records = Array(@object.send(association[:name])) + associations[association[:name]] = records.collect(&:id) end - associations end + associations end + + + + +end end + + diff --git a/app/views/rails_admin/main/export.html.haml b/app/views/rails_admin/main/export.html.haml index 552143184a..f80794f4c8 100644 --- a/app/views/rails_admin/main/export.html.haml +++ b/app/views/rails_admin/main/export.html.haml @@ -1,112 +1,12 @@ - params = request.params.except(:action, :controller, :utf8, :page, :per_page, :format, :authenticity_token) -- visible_fields = @model_config.export.with(:view => self, :object => @abstract_model.model.new).visible_fields - - -- specs = @model_config.export.with(:view => self, :object => @abstract_model.model.new).extract_specs - -- specs_fields = @model_config.export.with(:view => self, :object => @abstract_model.model.new).spec_fields specs[0] - - - = form_tag export_path(params.merge(:all => true)), :method => 'post', :class => 'form-horizontal denser' do - %input{:name => "send_data", :type => "hidden", :value => "true"}/ - %fieldset - %legend - %i.icon-chevron-down - = t('admin.export.select') - - .control-group - %label.control-label{:rel => 'tooltip', :'data-original-title' => t('admin.export.click_to_reverse_selection'), :onclick => 'jQuery(this).siblings(".controls").find("input").click()'}= t('admin.export.fields_from', :name => @model_config.label_plural.downcase) - .controls - - visible_fields.select{ |f| !f.spec? }.each do |field| - - list = field.virtual? ? 'methods' : 'only' - - if field.association? && field.association[:polymorphic] - %label.checkbox{:for => "schema_#{list}_#{field.method_name}"} - = check_box_tag "schema[#{list}][]", field.method_name, true, { :id => "schema_#{list}_#{field.method_name}" } - = field.label + " [id]" - - polymorphic_type_column_name = @abstract_model.properties.find {|p| field.association[:foreign_type] == p[:name] }[:name] - %label.checkbox{:for => "schema_#{list}_#{polymorphic_type_column_name}"} - = check_box_tag "schema[#{list}][]", polymorphic_type_column_name, true, { :id => "schema_#{list}_#{polymorphic_type_column_name}" } - = field.label + " [type]" - - else - %label.checkbox{:for => "schema_#{list}_#{field.name}"} - = check_box_tag "schema[#{list}][]", field.name, true, { :id => "schema_#{list}_#{field.name}" } - = field.label - - - - - specs.each do |spec| - - spec_fields = @model_config.export.with(:view => self, :object => @abstract_model.model.new).spec_fields spec - .control-group - %label.control-label{:rel => 'tooltip', :'data-original-title' => t('admin.export.click_to_reverse_selection'), :onclick => 'jQuery(this).siblings(".controls").find("input").click()'}= t('admin.export.fields_from', :name => spec) -
-
-
- .controls - - spec_fields.select{ |f| !f.association? || f.association[:polymorphic] }.each do |field| - - list = field.virtual? ? 'methods' : 'only' - - if field.association? && field.association[:polymorphic] - %label.checkbox{:for => "schema_#{list}_#{field.method_name}"} - = check_box_tag "schema[#{list}][]", field.method_name, true, { :id => "schema_#{list} _#{field.method_name}" } - = field.label + " [id]" - - polymorphic_type_column_name = @abstract_model.properties.find {|p| field.association[:foreign_type] == p[:name] }[:name] - %label.checkbox{:for => "schema_#{list}_#{polymorphic_type_column_name}"} - = check_box_tag "schema[#{list}][]", polymorphic_type_column_name, true, { :id => "schema_#{list}_#{polymorphic_type_column_name}" } - = field.label + " [type]" - - else - %label.checkbox{:for => "schema_#{list}_#{field.name}"} - = check_box_tag "schema[#{list}][]", field.name, true, { :id => "schema_#{list}_#{field.name}" } - = field.label - - - - - - visible_fields.select{ |f| f.association? && !f.association[:polymorphic] }.each do |field| - - fields = field.associated_model_config.export.with(:view => self, :object => (associated_model = field.associated_model_config.abstract_model.model).new).new_fields.select{ |f| !f.association? } - .control-group - %label.control-label{:rel => 'tooltip', :'data-original-title' => t('admin.export.click_to_reverse_selection'), :onclick => 'jQuery(this).siblings(".controls").find("input").click()'}= t('admin.export.fields_from_associated', :name => field.label.downcase) - .controls - - fields.each do |associated_model_field| - - list = associated_model_field.virtual? ? 'methods' : 'only' - %label.checkbox{:for => "schema_include_#{field.name}_#{list}_#{associated_model_field.name}"} - = check_box_tag "schema[include][#{field.name}][#{list}][]", associated_model_field.name, true, { :id => "schema_include_#{field.name}_#{list}_#{associated_model_field.name}" } - = associated_model_field.label - - %fieldset - %legend - %i.icon-chevron-down - = t('admin.export.options_for', :name => 'csv') - .control-group - - guessed_encoding = (Rails.configuration.database_configuration[Rails.env]['encoding'].presence rescue 'UTF-8') || 'UTF-8' - %label.control-label{:for => "csv_options_encoding_to"}= t('admin.export.csv.encoding_to') - .controls - -# from http://books.google.com/support/partner/bin/answer.py?answer=30990 : - = select_tag 'csv_options[encoding_to]', options_for_select(["", "UTF-8", "UTF-16LE", "UTF-16BE", "UTF-32LE", "UTF-32BE", "UTF-7", "ISO-8859-1", "ISO-8859-15", "IBM-850", "MacRoman", "Windows-1252", "ISO-8859-3", "IBM-852", "ISO-8859-2", "MacCE", "Windows-1250", "IBM-855", "ISO-8859-5", "ISO-IR-111", "KOI8-R", "MacCyrillic", "Windows-1251", "CP-866", "KOI-U", "MacUkranian", "GB2312", "GBK", "GB18030", "HZ", "ISO-2022-CN", "Big5", "Big5-HKSCS", "EUC-TW", "EUC-JP", "ISO-2022-JP", "Shift_JIS", "EUC-KR", "UHC", "JOHAB", "ISO-2022-KR"]) - %p.help-block= t('admin.export.csv.encoding_to_help', :name => guessed_encoding) - - .control-group - %label.control-label{:for => "csv_options_skip_header"}= t('admin.export.csv.skip_header') - .controls - = check_box_tag 'csv_options[skip_header]', 'true' - %p.help-block= t('admin.export.csv.skip_header_help') - - .control-group - %label.control-label{:for => "csv_options_generator_col_sep"}= t('admin.export.csv.col_sep') - .controls - = select_tag 'csv_options[generator][col_sep]', options_for_select({ '' => t('admin.export.csv.default_col_sep'), " ','" => ',', " ';'" => ';', '' => "'\t'" }) - %p.help-block= t('admin.export.csv.col_sep_help', :value => t('admin.export.csv.default_col_sep')) - .form-actions - %input{:type => :hidden, :name => 'return_to', :value => (params[:return_to].presence || request.referer)} + Confirm Exporting of Selected Items: %button.btn.btn-primary{:type => "submit", :name => 'csv'} %i.icon-white.icon-ok = t("admin.export.confirmation", :name => 'csv') - %button.btn.btn-info{:type => "submit", :name => 'json'} - = t("admin.export.confirmation", :name => 'json') - %button.btn.btn-info{:type => "submit", :name => 'xml'} - = t("admin.export.confirmation", :name => 'xml') %button.btn{:type => "submit", :name => "_continue"} %i.icon-remove = t("admin.form.cancel") diff --git a/app/views/rails_admin/main/import.html.erb b/app/views/rails_admin/main/import.html.erb new file mode 100644 index 0000000000..c6bf9d7dbf --- /dev/null +++ b/app/views/rails_admin/main/import.html.erb @@ -0,0 +1,35 @@ +

File Upload

+<%= form_for :upload_file, :url=>{ :action=>"import"}, :html => { :multipart => true } do |f| %> + + + + + + + + + + + + + +
+ + + <%= f.file_field :file%> +
+ + + <%= f.select :category, options_for_select(["Category"]+Tag.first_level_categorization_nodes.map(&:tagname).sort) %> + +
+ +
+<% end %> + diff --git a/app/views/rails_admin/main/index.html.haml b/app/views/rails_admin/main/index.html.haml index 6096c793ab..03aa51d34b 100644 --- a/app/views/rails_admin/main/index.html.haml +++ b/app/views/rails_admin/main/index.html.haml @@ -136,25 +136,35 @@ .well %span#filters_box + -if @abstract_model.to_param.to_s.eql? "flag" ="Object Type: " %select{:name => 'object_type'} -items.each do |c| %option{:value=>c, selected: params[:object_type] == c} =h c -
+

="Reason: " %select{:name => 'reason'} -reasons.each do |c| %option{:value=>c, selected: params[:reason] == c} =h c -
+

="Status: " %select{:name => 'status'} -status.each do |c| %option{:value=>c, selected: params[:status] == c} =h c -
+

+ -if @abstract_model.to_param.to_s.eql? "item" + ="Category: " + %select{:name => 'category_cache'} + -Tag.first_level_categorization_nodes.map(&:tagname).sort.each do |c| + %option{:value=>c, selected: params[:category_cache] == c} + =h c +

+ + %hr.filters_box{:style => "display:#{@ordered_filters.empty? ? 'none' : 'block'}"} %input.input-small{:name => "query", :type => "search", :value => query, :placeholder => t("admin.misc.filter")} %button.btn.btn-primary{:type => "submit", :'data-disable-with' => " ".html_safe + t("admin.misc.refresh")} diff --git a/config/initializers/rails_admin.rb b/config/initializers/rails_admin.rb index 785aad1912..0f567e4319 100644 --- a/config/initializers/rails_admin.rb +++ b/config/initializers/rails_admin.rb @@ -1,6 +1,7 @@ require 'rails_admin/config/actions/merge_tags' require 'rails_admin/config/actions/merge_items' require 'rails_admin/config/actions/moderate' +require 'rails_admin/config/actions/import' module RailsAdmin module Config @@ -14,6 +15,12 @@ class MergeItems < RailsAdmin::Config::Actions::Base class Moderate < RailsAdmin::Config::Actions::Base RailsAdmin::Config::Actions.register(self) end + class Import < RailsAdmin::Config::Actions::Base + RailsAdmin::Config::Actions.register(self) + end + class Export < RailsAdmin::Config::Actions::Base + RailsAdmin::Config::Actions.register(self) + end end end end @@ -26,21 +33,29 @@ class Moderate < RailsAdmin::Config::Actions::Base show edit delete - export + export do + visible do + bindings[:abstract_model].model.to_s == "Item" + end + end + import do + visible do + bindings[:abstract_model].model.to_s == "Item" + end + end show_in_app bulk_delete - merge_tags do + merge_tags do visible do bindings[:abstract_model].model.to_s == "Tag" end end - moderate do + moderate do visible do bindings[:abstract_model].model.to_s == "Item" || bindings[:abstract_model].model.to_s == "Tag" || bindings[:abstract_model].model.to_s == "Photo" end end - - merge_items do + merge_items do visible do bindings[:abstract_model].model.to_s == "Item" end diff --git a/config/locales/rails_admin.en.yml b/config/locales/rails_admin.en.yml index f8595501f2..fd5326eaed 100644 --- a/config/locales/rails_admin.en.yml +++ b/config/locales/rails_admin.en.yml @@ -95,6 +95,13 @@ en: link: "Export found %{model_label_plural}" bulk_link: "Export selected %{model_label_plural}" done: "exported" + import: + title: "Import" + menu: "Import" + breadcrumb: "Import" + link: "Import" + bulk_link: "Import" + done: "File successfully uploaded! Check your email for import report. " history_index: title: "History for %{model_label_plural}" menu: "History" @@ -122,6 +129,7 @@ en: new_model: "%{name} (new)" merge_items: "The following Items will be merged into two items" merge_tags: "The following Tags will be merged into one tag" + import: "Upload Items from CSV file" export: confirmation: "Export to %{name}" select: "Select fields to export" diff --git a/lib/rails_admin/config/actions.rb b/lib/rails_admin/config/actions.rb index 3296907ead..77d9b96c2f 100644 --- a/lib/rails_admin/config/actions.rb +++ b/lib/rails_admin/config/actions.rb @@ -117,5 +117,4 @@ def init_actions! require 'rails_admin/config/actions/edit' require 'rails_admin/config/actions/export' require 'rails_admin/config/actions/delete' -require 'rails_admin/config/actions/bulk_delete' - +require 'rails_admin/config/actions/bulk_delete' \ No newline at end of file diff --git a/lib/rails_admin/config/actions/export.rb b/lib/rails_admin/config/actions/export.rb index 887c0f7a1e..9b82e1b01c 100644 --- a/lib/rails_admin/config/actions/export.rb +++ b/lib/rails_admin/config/actions/export.rb @@ -13,17 +13,17 @@ class Export < RailsAdmin::Config::Actions::Base end register_instance_option :controller do - Proc.new do - if format = params[:json] && :json || params[:csv] && :csv || params[:xml] && :xml - request.format = format - @schema = params[:schema].symbolize if params[:schema] # to_json and to_xml expect symbols for keys AND values. - @objects = list_entries(false, @model_config, :export) - index - else - render @action.template_name - end + Proc.new do + if request.post? + @ids = list_entries(false, @model_config, :export).entries.map(& :_id) + Exporter::Exporter.new.export_query_items @ids, current_person + flash[:success] = "#{@ids.count} items will be exported, you will recieve an email with export results shortly." + redirect_to back_or_index + else + render @action.template_name + end end end diff --git a/lib/rails_admin/config/actions/import.rb b/lib/rails_admin/config/actions/import.rb new file mode 100644 index 0000000000..ccc7538488 --- /dev/null +++ b/lib/rails_admin/config/actions/import.rb @@ -0,0 +1,51 @@ +require Rails.root.join('lib/importer', 'generic_importer.rb') +require Rails.root.join('lib/exporter', 'exporter.rb') +require Rails.root.join('lib', 'association_manager.rb') + + +module RailsAdmin + module Config + module Actions + class Import < RailsAdmin::Config::Actions::Base + RailsAdmin::Config::Actions.register(self) + + register_instance_option :collection do + true + end + + register_instance_option :http_methods do + [:get, :post] + end + + register_instance_option :controller do + Proc.new do + if request.post? # Uploading File + csv_file = params[:upload_file][:file] + category = params[:upload_file][:category] + + if csv_file != nil && category != 'Category' + importer = Importer::GenericImporter.new category + importer.upload_spreadsheet csv_file, current_person + flash[:success] = "Success ! The file:"+csv_file.original_filename+" containing "+ category.to_s+" was uploaded and is ready for import. You will recieve an email with import results once the file import is processed." + elsif csv_file == nil + flash[:error] = "Opps! You forgot to select the file to be imported !" + else + flash[:error] = "Opps! You forgot to select the item categoryegory for your file: "+csv_file.original_filename+"!" + + end + end + end + end + + + register_instance_option :bulkable? do + false + end + + register_instance_option :link_icon do + 'icon-folder-open' + end + end + end +end +end