From e0f7d92d7a777c075de0a8d43ea83d1640e93395 Mon Sep 17 00:00:00 2001 From: manson Date: Mon, 5 May 2014 19:33:35 +0800 Subject: [PATCH] Web Resource module --- app/controllers/admin/web_links_controller.rb | 4 - .../admin/web_resources_controller.rb | 94 ++++++++++++ app/controllers/web_resources_controller.rb | 31 ++++ app/models/web_link.rb | 30 +++- app/views/admin/web_links/index.html.erb | 2 - app/views/admin/web_resources/_form.html.erb | 139 ++++++++++++++++++ app/views/admin/web_resources/edit.html.erb | 5 + app/views/admin/web_resources/index.html.erb | 89 +++++++++++ .../admin/web_resources/index_table.html.erb | 32 ++++ app/views/admin/web_resources/new.html.erb | 5 + app/views/web_resources/index.html.erb | 1 + app/views/web_resources/show.html.erb | 1 + config/routes.rb | 7 +- lib/links/engine.rb | 30 ++-- 14 files changed, 450 insertions(+), 20 deletions(-) delete mode 100644 app/controllers/admin/web_links_controller.rb create mode 100644 app/controllers/admin/web_resources_controller.rb create mode 100644 app/controllers/web_resources_controller.rb delete mode 100644 app/views/admin/web_links/index.html.erb create mode 100644 app/views/admin/web_resources/_form.html.erb create mode 100644 app/views/admin/web_resources/edit.html.erb create mode 100644 app/views/admin/web_resources/index.html.erb create mode 100644 app/views/admin/web_resources/index_table.html.erb create mode 100644 app/views/admin/web_resources/new.html.erb create mode 100644 app/views/web_resources/index.html.erb create mode 100644 app/views/web_resources/show.html.erb diff --git a/app/controllers/admin/web_links_controller.rb b/app/controllers/admin/web_links_controller.rb deleted file mode 100644 index 23486ee..0000000 --- a/app/controllers/admin/web_links_controller.rb +++ /dev/null @@ -1,4 +0,0 @@ -class Admin::WebLinksController < ApplicationController - def index - end -end diff --git a/app/controllers/admin/web_resources_controller.rb b/app/controllers/admin/web_resources_controller.rb new file mode 100644 index 0000000..526e89b --- /dev/null +++ b/app/controllers/admin/web_resources_controller.rb @@ -0,0 +1,94 @@ +class Admin::WebResourcesController < OrbitAdminController + before_action ->(module_app = @app_title) { set_variables module_app } + before_action :set_link, only: [:edit, :update, :destroy] + + def index + @tags = @module_app.tags + @categories = @module_app.categories + + @filter_fields = { + :status=>[{:title=>"is_top",:id=>"is_top"},{:title=>"is_hot",:id=>"is_hot"},{:title=>"is_hidden",:id=>"is_hidden"}], + :category=>@categories.map{|c| {:title=>c.title, :id=>c.id}}, + :tags=>@tags.map{|tag| {:title=>tag.name, :id=>tag.id}} + } + end + + def index_table + status = params[:filters][:status].blank? ? [] : params[:filters][:status] rescue [] + categories = params[:filters][:category].blank? ? [] : params[:filters][:category] rescue [] + tags = params[:filters][:tags].blank? ? [] : params[:filters][:tags] rescue [] + + @links = Kaminari.paginate_array( + WebLink.order_by(sort).with_categories(categories).with_tags(tags).with_status(status) + ).page(params[:page]).per(10) + + @table_fields = [:status, :category, :title] + render :layout => false + end + + def sort + unless params[:sort].blank? + case params[:sort] + when "status" + @sort = [[:is_top, params[:order]], + [:is_hot, params[:order]], + [:is_hidden,params[:order]]] + when "category" + @sort = {:category_id=>params[:order]} + when "title" + @sort = {:title=>params[:order]} + when "start_date" + @sort = {:postdate=>params[:order]} + when "end_date" + @sort = {:deadline=>params[:order]} + when "last_modified" + @sort = {:update_user_id=>params[:order]} + end + else + @sort = {:created_at=>'desc'} + end + @sort + end + + def new + @tags =@module_app.tags + @categories = @module_app.categories + @statuses = [] + @link = WebLink.new + end + + def create + link = WebLink.new(link_params) + link.create_user_id = current_user.id + link.update_user_id = current_user.id + link.save + redirect_to admin_web_resources_path + end + + def edit + @tags =@module_app.tags + @categories = @module_app.categories + @statuses = [] + end + + def update + @link.update_attributes(link_params) + @link.save + redirect_to admin_web_resources_path + end + + def destroy + @link.destroy + redirect_to admin_web_resources_path + end + + private + + def set_link + @link = WebLink.find(params[:id]) + end + + def link_params + params.require(:web_link).permit! + end +end diff --git a/app/controllers/web_resources_controller.rb b/app/controllers/web_resources_controller.rb new file mode 100644 index 0000000..d131ee8 --- /dev/null +++ b/app/controllers/web_resources_controller.rb @@ -0,0 +1,31 @@ +class WebResourcesController < ApplicationController + + def index + links = WebLink.order_by(:created_at=>'desc') + web_link = links.collect do |link| + { + "title" => link.title, + "link_to_show" => link.url + } + end + { + "data" => web_link, + "extras" => {"widget-title"=>"Web Resource"} + } + end + + def widget + links = WebLink.order_by(:created_at=>'desc') + web_link = links.collect do |link| + { + "title" => link.title, + "link_to_show" => link.url + } + end + { + "data" => web_link, + "extras" => {"widget-title"=>"Web Resource"} + } + end + +end diff --git a/app/models/web_link.rb b/app/models/web_link.rb index d035c59..5ab0740 100644 --- a/app/models/web_link.rb +++ b/app/models/web_link.rb @@ -1,4 +1,30 @@ +# encoding: utf-8 class WebLink include Mongoid::Document - field :title, type: String -end + include Mongoid::Timestamps + + include OrbitModel::Status + include OrbitTag::Taggable + include OrbitCategory::Categorizable + + field :title, localize: true + field :context, localize: true + + field :url, localize: true + field :create_user_id + field :update_user_id + + scope :can_display, ->{where(is_hidden: false)} + + before_validation :add_http + validates :url, :presence => true, :format => /\A(http|https):\/\/(([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(:[0-9]{1,5})?(\/.*)?\Z/i + + protected + + def add_http + unless self.url[/^http:\/\//] || self.url[/^https:\/\//] + self.url = 'http://' + self.url + end + end + +end \ No newline at end of file diff --git a/app/views/admin/web_links/index.html.erb b/app/views/admin/web_links/index.html.erb deleted file mode 100644 index 1fab4b7..0000000 --- a/app/views/admin/web_links/index.html.erb +++ /dev/null @@ -1,2 +0,0 @@ -

Admin::WebLinks#index

-

Find me in app/views/admin/web_links/index.html.erb

diff --git a/app/views/admin/web_resources/_form.html.erb b/app/views/admin/web_resources/_form.html.erb new file mode 100644 index 0000000..09d3622 --- /dev/null +++ b/app/views/admin/web_resources/_form.html.erb @@ -0,0 +1,139 @@ +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag "lib/main-forms" %> +<% end %> +<% content_for :page_specific_javascript do %> + <%= javascript_include_tag "lib/bootstrap-fileupload" %> + <%= javascript_include_tag "lib/module-area" %> +<% end %> + + +
+ + + + + + +
+ + +
+ + +
+ +
+ <%= f.select :category_id, @categories.collect{|t| [ t.title, t.id ]} %> +
+
+ +
+ + +
+ + +
+ +
+ + + +
+
+ +
+ <%# end %> + + +
+ + +
+ +
+ <% @tags.each do |tag| %> + + <% end %> +
+
+ +
+ + +
+ + + + + + +
+ + <% Site.first.in_use_locales.each_with_index do |locale, i| %> + +
"> + + +
+ +
+ <%= f.fields_for :title_translations do |f| %> + <%= f.text_field locale, class: "input-block-level", placeholder: t(:title), value: (@link.title_translations[locale] rescue nil) %> + <% end %> +
+
+ + +
+ +
+ <%= f.fields_for :context_translations do |f| %> + <%= f.text_area locale, rows: 5, class: "input-block-level", value: (@link.context_translations[locale] rescue nil) %> + <% end %> +
+
+ + +
+ +
+ <%= f.fields_for :url_translations do |f| %> + <%= f.text_field locale, class: "input-block-level", placeholder: t(:url), value: (@link.url_translations[locale] rescue nil) %> + <% end %> +
+
+ +
+ + <% end %> + +
+ +
+ + +
+ <%= f.submit t('submit'), class: 'btn btn-primary' %> + <%= link_to t('cancel'), admin_web_resources_path, :class=>"btn" %> +
+ + \ No newline at end of file diff --git a/app/views/admin/web_resources/edit.html.erb b/app/views/admin/web_resources/edit.html.erb new file mode 100644 index 0000000..c17f213 --- /dev/null +++ b/app/views/admin/web_resources/edit.html.erb @@ -0,0 +1,5 @@ +<%= form_for @link, url: admin_web_resource_path(@link), html: {class: "form-horizontal main-forms previewable"} do |f| %> +
+ <%= render :partial => 'form', locals: {f: f} %> +
+<% end %> \ No newline at end of file diff --git a/app/views/admin/web_resources/index.html.erb b/app/views/admin/web_resources/index.html.erb new file mode 100644 index 0000000..f846c56 --- /dev/null +++ b/app/views/admin/web_resources/index.html.erb @@ -0,0 +1,89 @@ +<% content_for :right_nav do %> + +
+ <% @filter_fields.keys.each do |field| %> +
+
+ <% @filter_fields[field].each do |val| %> + <%= link_to t(val[:title]), "#", :onclick => "addFilter('filters[#{field}][]=#{val[:id]}')", :class => "btn btn-small #{is_filter_active?(field, val[:id])}" %> + <% end %> +
+ +
+ <% end %> +
+<% end %> + + + + +<%= render 'layouts/delete_modal', delete_options: @delete_options %> + + \ No newline at end of file diff --git a/app/views/admin/web_resources/index_table.html.erb b/app/views/admin/web_resources/index_table.html.erb new file mode 100644 index 0000000..30518c7 --- /dev/null +++ b/app/views/admin/web_resources/index_table.html.erb @@ -0,0 +1,32 @@ + + + + <% @table_fields.each do |f| %> + <%= thead(f) %> + <% end %> + + + + <% @links.each do |link| %> + + + + + + <% end %> + +
<%= link.status_for_table %><%= link.category.title %> + <%= link.title %> + +
+ +<%= + content_tag :div, class: "bottomnav clearfix" do + content_tag :div, paginate(@links), class: "pagination pagination-centered" + end +%> \ No newline at end of file diff --git a/app/views/admin/web_resources/new.html.erb b/app/views/admin/web_resources/new.html.erb new file mode 100644 index 0000000..488e772 --- /dev/null +++ b/app/views/admin/web_resources/new.html.erb @@ -0,0 +1,5 @@ +<%= form_for @link, url: admin_web_resources_path, html: {class: "form-horizontal main-forms previewable"} do |f| %> +
+ <%= render :partial => 'form', locals: {f: f} %> +
+<% end %> \ No newline at end of file diff --git a/app/views/web_resources/index.html.erb b/app/views/web_resources/index.html.erb new file mode 100644 index 0000000..648b75c --- /dev/null +++ b/app/views/web_resources/index.html.erb @@ -0,0 +1 @@ +<%= render_view %> \ No newline at end of file diff --git a/app/views/web_resources/show.html.erb b/app/views/web_resources/show.html.erb new file mode 100644 index 0000000..648b75c --- /dev/null +++ b/app/views/web_resources/show.html.erb @@ -0,0 +1 @@ +<%= render_view %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 3f025dd..24bb10b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,9 +3,10 @@ Rails.application.routes.draw do locales = Site.find_by(site_active: true).in_use_locales rescue I18n.available_locales scope "(:locale)", locale: Regexp.new(locales.join("|")) do - namespace :admin do - resources :web_links - end + namespace :admin do + get 'web_resources/index_table' => 'web_resources#index_table' + resources :web_resources + end end diff --git a/lib/links/engine.rb b/lib/links/engine.rb index 508db7c..ae01722 100644 --- a/lib/links/engine.rb +++ b/lib/links/engine.rb @@ -1,8 +1,8 @@ -module Links +module WebResource class Engine < ::Rails::Engine - initializer "links" do - OrbitApp.registration "Links", :type => "ModuleApp" do - module_label "links.links" + initializer "web_resource" do + OrbitApp.registration "WebResource", :type => "ModuleApp" do + module_label "link" base_url File.expand_path File.dirname(__FILE__) taggable "WebLink" @@ -10,12 +10,24 @@ module Links authorizable side_bar do - head_label_i18n 'faq.faq', icon_class: "icons-link" + head_label_i18n 'link',:icon_class=>"icons-link" available_for [:admin,:manager,:sub_manager] - active_for_controllers ({:private=>['web_links']}) - head_link_path "admin_web_links_path" - end + active_for_controllers ({:private=>['web_resource']}) + head_link_path "admin_web_resources_path" + + context_link 'list_', + :link_path=>"admin_web_resources_path" , + :priority=>1, + :active_for_action=>{'admin/web_resources'=>'index'}, + :available_for => [:all] + + context_link 'add', + :link_path=>"new_admin_web_resource_path" , + :priority=>2, + :active_for_action=>{'admin/web_resources'=>'new'}, + :available_for => [:sub_manager] + end end - end + end end end