From 85cd87fd8f331c396a746a7dbb1f123e7900e602 Mon Sep 17 00:00:00 2001 From: Praffesor Date: Tue, 20 Feb 2018 18:28:25 +0200 Subject: [PATCH 001/114] added title option, fixed images without light resizer, small sequel fixes --- app/assets/javascripts/form/form-ajax.coffee | 2 +- .../rademade_admin/abstract_controller.rb | 11 +++++--- app/helpers/rademade_admin/form_helper.rb | 7 +++-- app/models/rademade_admin/ability.rb | 26 ------------------- app/services/login.rb | 3 +-- app/services/saver.rb | 12 ++++++--- app/services/upload/preview/file.rb | 10 ++++--- .../rademade_admin/_blocks/_form.html.erb | 7 +++-- .../rademade_admin/_blocks/_list.html.erb | 8 +++--- .../_blocks/_navigation.html.erb | 6 +++-- .../_blocks/_pagination.html.erb | 2 +- .../_blocks/button/_save_and_return.html.erb | 5 ++-- .../_blocks/table/_head.html.erb | 2 +- .../rademade_admin/dashboard/index.html.erb | 10 ++++--- config/locales/rademade_admin.en.yml | 4 +-- .../model/adapter/query/sequel.rb | 2 +- lib/rademade_admin/model/configuration.rb | 13 ++++++++-- lib/rademade_admin/model/info.rb | 12 ++++++++- lib/rademade_admin/model/info/uploader.rb | 9 ++++++- 19 files changed, 88 insertions(+), 63 deletions(-) diff --git a/app/assets/javascripts/form/form-ajax.coffee b/app/assets/javascripts/form/form-ajax.coffee index 98824083..b0636b74 100644 --- a/app/assets/javascripts/form/form-ajax.coffee +++ b/app/assets/javascripts/form/form-ajax.coffee @@ -4,7 +4,7 @@ class @FormAjax extends Backbone.View @_prepareForAjax() $.ajax( url : @$el.attr('action') - type : @$el.attr('method') + type : @$el.data('method') data : @_getData() dataType : 'json' ).done(@_onDone).fail(@_onFail).always(@_onAlways) diff --git a/app/controllers/rademade_admin/abstract_controller.rb b/app/controllers/rademade_admin/abstract_controller.rb index 6e5939ed..a4af29b1 100644 --- a/app/controllers/rademade_admin/abstract_controller.rb +++ b/app/controllers/rademade_admin/abstract_controller.rb @@ -6,6 +6,8 @@ class AbstractController < ApplicationController layout 'rademade_admin' + protect_from_forgery prepend: true + before_action :init_user, :init_template_service, :require_login attr_reader :current_user @@ -21,7 +23,10 @@ def require_login end def init_user - @current_user = RademadeAdmin.configuration.admin_class.find(session[:user_id]) if session[:user_id].present? + if session[:user_id].present? + related_info = RademadeAdmin::Model::Graph.instance.model_info(RademadeAdmin.configuration.admin_class) + @current_user = related_info.query_adapter.find(session[:user_id]) + end end def init_template_service @@ -36,7 +41,7 @@ def render_errors(errors) end def admin_logged_in? - @current_user.is_a? RademadeAdmin.configuration.admin_class and @current_user.admin? + @current_user && @current_user.admin? end def current_ability @@ -44,4 +49,4 @@ def current_ability end end -end \ No newline at end of file +end diff --git a/app/helpers/rademade_admin/form_helper.rb b/app/helpers/rademade_admin/form_helper.rb index 18408abb..e36bd72c 100644 --- a/app/helpers/rademade_admin/form_helper.rb +++ b/app/helpers/rademade_admin/form_helper.rb @@ -45,7 +45,10 @@ def admin_form_options(record, model_info) :url => url, :method => method, :as => :data, - :html => admin_form_html_attributes(form_class) + :html => admin_form_html_attributes(form_class), + :data => { + :method => method + } } end @@ -111,4 +114,4 @@ def can_read_relation(data_item) !data_item.has_relation? || can?(:read, data_item.relation.to) end -end \ No newline at end of file +end diff --git a/app/models/rademade_admin/ability.rb b/app/models/rademade_admin/ability.rb index 4cad4930..bd725092 100644 --- a/app/models/rademade_admin/ability.rb +++ b/app/models/rademade_admin/ability.rb @@ -7,32 +7,6 @@ def initialize(user) if user.try(:admin?) can :manage, :all end - # Define abilities for the passed in user here. For example: - # - # user ||= User.new # guest user (not logged in) - # if user.admin? - # can :manage, :all - # else - # can :read, :all - # end - # - # The first argument to `can` is the action you are giving the user - # permission to do. - # If you pass :manage it will apply to every action. Other common actions - # here are :read, :create, :update and :destroy. - # - # The second argument is the resource the user can perform the action on. - # If you pass :all it will apply to every resource. Otherwise pass a Ruby - # class of the resource. - # - # The third argument is an optional hash of conditions to further filter the - # objects. - # For example, here the user can only update published articles. - # - # can :update, Article, :published => true - # - # See the wiki for details: - # https://github.com/ryanb/cancan/wiki/Defining-Abilities end end diff --git a/app/services/login.rb b/app/services/login.rb index bae29744..4346cf6d 100644 --- a/app/services/login.rb +++ b/app/services/login.rb @@ -6,9 +6,8 @@ def self.admin(params) params.require(:data).permit([:email, :password]) user = RademadeAdmin.configuration.admin_class.get_by_email(params[:data][:email]) - error(I18n.t('rademade_admin.login.validation.email_not_found'), :email) unless user.is_a? RademadeAdmin.configuration.admin_class + error(I18n.t('rademade_admin.login.validation.email_not_found'), :email) if user.nil? error(I18n.t('rademade_admin.login.validation.incorrect_password'), :password) unless user.valid_password? params[:data][:password] - error(I18n.t('rademade_admin.login.validation.access_denied'), :email) unless user.admin? user end diff --git a/app/services/saver.rb b/app/services/saver.rb index 21b1f917..edef419d 100644 --- a/app/services/saver.rb +++ b/app/services/saver.rb @@ -82,8 +82,12 @@ def save_model_upload(data_item, image_path) if image_path.blank? item.instance_exec(&data_item.uploader.remove_proc) else - full_image_path = data_item.uploader.full_path_for(image_path) - data_item.set_data(entity, File.open(full_image_path)) + if image_path.match /^http/ # is full path already + item.instance_exec(&data_item.uploader.remote_url_setter_proc(image_path)) + else + full_image_path = data_item.uploader.full_path_for(image_path) + data_item.set_data(entity, File.open(full_image_path)) + end end rescue # rm_todo clear image @@ -99,7 +103,7 @@ def find_entities(data_item, ids) end def simple_field_params - @params.require(:data).symbolize_keys.slice(*@model_info.data_items.save_form_fields) + @params.require(:data).slice(*@model_info.data_items.save_form_fields) end def related_entities(data_item, ids) @@ -107,4 +111,4 @@ def related_entities(data_item, ids) end end -end \ No newline at end of file +end diff --git a/app/services/upload/preview/file.rb b/app/services/upload/preview/file.rb index 2af3be2f..39fe08bb 100644 --- a/app/services/upload/preview/file.rb +++ b/app/services/upload/preview/file.rb @@ -35,13 +35,17 @@ def crop? end def image_preview - @uploader.resize_with_crop(300, 300) + if image? + @uploader.resize_with_crop(300, 300) + else + @uploader.url + end end protected def uploaded_preview_html - if image? + if image? || @uploader.file.content_type.match(/image\//) image_preview_html elsif video? video_preview_html @@ -87,4 +91,4 @@ def remove_ico_html end end end -end \ No newline at end of file +end diff --git a/app/views/rademade_admin/_blocks/_form.html.erb b/app/views/rademade_admin/_blocks/_form.html.erb index 10add560..ff4860dc 100644 --- a/app/views/rademade_admin/_blocks/_form.html.erb +++ b/app/views/rademade_admin/_blocks/_form.html.erb @@ -29,8 +29,11 @@ end if @model_info.persistence_adapter.new?(@item) content_header = t('rademade_admin.title.new_record', :model => @model_info.item_name) else - content_header = t('rademade_admin.title.edit_record', :model => @model_info.item_name, :record => @item) + content_header = t('rademade_admin.title.edit_record', { + :model => @model_info.item_name, + :record => @model_info.title_of(@item) + }) end concat render :template => @template_service.content_item_layout, :locals => { :content_header => content_header -} %> \ No newline at end of file +} %> diff --git a/app/views/rademade_admin/_blocks/_list.html.erb b/app/views/rademade_admin/_blocks/_list.html.erb index f897f715..64f2ec78 100644 --- a/app/views/rademade_admin/_blocks/_list.html.erb +++ b/app/views/rademade_admin/_blocks/_list.html.erb @@ -14,7 +14,7 @@ end %> <% content_for :content do %>
<%= yield(:nav_buttons) %>
<% - concat render @template_service.search_block + # concat render @template_service.search_block %>
<% @@ -23,11 +23,11 @@ end %> %>
<% - concat render @template_service.pagination_block - concat render @template_service.on_page_switcher_block + # concat render @template_service.pagination_block + # concat render @template_service.on_page_switcher_block %>
<% end concat render :template => @template_service.content_item_layout, :locals => { :content_header => @model_info.item_name -} %> \ No newline at end of file +} %> diff --git a/app/views/rademade_admin/_blocks/_navigation.html.erb b/app/views/rademade_admin/_blocks/_navigation.html.erb index 8bc5295c..5a77bb3f 100644 --- a/app/views/rademade_admin/_blocks/_navigation.html.erb +++ b/app/views/rademade_admin/_blocks/_navigation.html.erb @@ -1,4 +1,6 @@ -