blacksand 2.2.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +27 -0
  4. data/app/assets/javascripts/blacksand/dashboard/navigations.form.js.erb +34 -0
  5. data/app/assets/javascripts/blacksand/dashboard/navigations.index.js +18 -0
  6. data/app/assets/javascripts/blacksand/dashboard/pages.form.js.erb +69 -0
  7. data/app/assets/javascripts/blacksand/dashboard/pages.index.js +17 -0
  8. data/app/assets/javascripts/blacksand/dashboard.js +14 -0
  9. data/app/assets/javascripts/blacksand/ie.js +2 -0
  10. data/app/assets/javascripts/bootstrap-datepicker.zh-CN.js +20 -0
  11. data/app/assets/stylesheets/blacksand/dashboard.scss +26 -0
  12. data/app/controllers/blacksand/dashboard/base_controller.rb +35 -0
  13. data/app/controllers/blacksand/dashboard/navigations_controller.rb +88 -0
  14. data/app/controllers/blacksand/dashboard/pages_controller.rb +176 -0
  15. data/app/controllers/blacksand/dashboard/prototypes_controller.rb +12 -0
  16. data/app/controllers/blacksand/dashboard/templates_controller.rb +8 -0
  17. data/app/controllers/blacksand/dashboard/users_controller.rb +78 -0
  18. data/app/controllers/blacksand_front/pages_controller.rb +17 -0
  19. data/app/helpers/blacksand/application_helper.rb +42 -0
  20. data/app/helpers/blacksand/page_helper.rb +11 -0
  21. data/app/helpers/blacksand_front/pages_helper.rb +20 -0
  22. data/app/models/blacksand/field.rb +35 -0
  23. data/app/models/blacksand/navigation.rb +24 -0
  24. data/app/models/blacksand/page.rb +194 -0
  25. data/app/models/blacksand/picture.rb +7 -0
  26. data/app/models/blacksand/property/as_array.rb +5 -0
  27. data/app/models/blacksand/property/file.rb +5 -0
  28. data/app/models/blacksand/property/gallery.rb +7 -0
  29. data/app/models/blacksand/property/slide.rb +5 -0
  30. data/app/models/blacksand/property.rb +76 -0
  31. data/app/models/blacksand/prototype.rb +11 -0
  32. data/app/models/blacksand/template.rb +9 -0
  33. data/app/models/kindeditor/asset.rb +14 -0
  34. data/app/models/kindeditor/file.rb +3 -0
  35. data/app/models/kindeditor/flash.rb +3 -0
  36. data/app/models/kindeditor/image.rb +3 -0
  37. data/app/models/kindeditor/media.rb +3 -0
  38. data/app/uploaders/blacksand/base_uploader.rb +16 -0
  39. data/app/uploaders/blacksand/file_uploader.rb +4 -0
  40. data/app/uploaders/blacksand/image_uploader.rb +78 -0
  41. data/app/uploaders/blacksand/slide_image_uploader.rb +66 -0
  42. data/app/views/blacksand/dashboard/navigations/_form.html.erb +23 -0
  43. data/app/views/blacksand/dashboard/navigations/edit.html.erb +3 -0
  44. data/app/views/blacksand/dashboard/navigations/index.html.erb +40 -0
  45. data/app/views/blacksand/dashboard/navigations/index.json.jbuilder +4 -0
  46. data/app/views/blacksand/dashboard/navigations/new.html.erb +3 -0
  47. data/app/views/blacksand/dashboard/navigations/reorder.js.erb +1 -0
  48. data/app/views/blacksand/dashboard/navigations/show.html.erb +4 -0
  49. data/app/views/blacksand/dashboard/navigations/show.json.jbuilder +1 -0
  50. data/app/views/blacksand/dashboard/pages/_fields.html.erb +89 -0
  51. data/app/views/blacksand/dashboard/pages/_form.html.erb +26 -0
  52. data/app/views/blacksand/dashboard/pages/_manage_list.html.erb +123 -0
  53. data/app/views/blacksand/dashboard/pages/_picture_fields.html.erb +18 -0
  54. data/app/views/blacksand/dashboard/pages/children_partial.js.erb +14 -0
  55. data/app/views/blacksand/dashboard/pages/edit.html.erb +20 -0
  56. data/app/views/blacksand/dashboard/pages/index.html.erb +27 -0
  57. data/app/views/blacksand/dashboard/pages/new.html.erb +20 -0
  58. data/app/views/blacksand/dashboard/pages/onchange_render.js.erb +14 -0
  59. data/app/views/blacksand/dashboard/pages/search.json.jbuilder +7 -0
  60. data/app/views/blacksand/dashboard/pages/show.html.erb +34 -0
  61. data/app/views/blacksand/dashboard/prototypes/index.html.erb +15 -0
  62. data/app/views/blacksand/dashboard/prototypes/show.html.erb +31 -0
  63. data/app/views/blacksand/dashboard/templates/index.html.erb +17 -0
  64. data/app/views/blacksand/dashboard/users/_form.html.erb +40 -0
  65. data/app/views/blacksand/dashboard/users/edit.html.erb +47 -0
  66. data/app/views/blacksand/dashboard/users/index.html.erb +37 -0
  67. data/app/views/blacksand/dashboard/users/index.json.jbuilder +4 -0
  68. data/app/views/blacksand/dashboard/users/new.html.erb +4 -0
  69. data/app/views/blacksand/dashboard/users/show.html.erb +2 -0
  70. data/app/views/blacksand/dashboard/users/show.json.jbuilder +1 -0
  71. data/app/views/layouts/blacksand/_navbar.html.erb +42 -0
  72. data/app/views/layouts/blacksand/dashboard.html.erb +33 -0
  73. data/config/locales/kaminari.zh-CN.yml +17 -0
  74. data/config/locales/nestable.zh-CN.yml +14 -0
  75. data/config/locales/zh-CN.yml +69 -0
  76. data/config/routes.rb +31 -0
  77. data/db/migrate/20151112055035_create_templates.rb +10 -0
  78. data/db/migrate/20151112055425_create_prototypes.rb +9 -0
  79. data/db/migrate/20151112055438_create_fields.rb +14 -0
  80. data/db/migrate/20151112055821_create_pages.rb +15 -0
  81. data/db/migrate/20151112060015_create_properties.rb +15 -0
  82. data/db/migrate/20151112060605_create_navigations.rb +13 -0
  83. data/db/migrate/20151209022305_create_kindeditor_assets.rb +17 -0
  84. data/db/migrate/20160513080702_create_pictures.rb +12 -0
  85. data/db/migrate/20170104014706_add_values_to_properties.rb +5 -0
  86. data/db/migrate/20170310092041_add_owner_type_to_kindeditor_asset.rb +5 -0
  87. data/db/migrate/20170322021913_add_options_to_template_and_prototype.rb +6 -0
  88. data/lib/blacksand/caching_pages.rb +16 -0
  89. data/lib/blacksand/cancancan.rb +19 -0
  90. data/lib/blacksand/controller_helper.rb +21 -0
  91. data/lib/blacksand/engine.rb +69 -0
  92. data/lib/blacksand/expire_pages.rb +21 -0
  93. data/lib/blacksand/my_json_type.rb +61 -0
  94. data/lib/blacksand/routing.rb +11 -0
  95. data/lib/blacksand/version.rb +3 -0
  96. data/lib/blacksand.rb +53 -0
  97. data/lib/generators/blacksand/install_generator.rb +30 -0
  98. data/lib/generators/blacksand/new_site_generator.rb +28 -0
  99. data/lib/tasks/blacksand_tasks.rake +72 -0
  100. data/lib/tasks/carrierwave_storage_migrate_to_qiniu.rake +25 -0
  101. metadata +541 -0
data/lib/blacksand.rb ADDED
@@ -0,0 +1,53 @@
1
+ require "blacksand/engine"
2
+ require "blacksand/cancancan"
3
+
4
+ require 'blacksand/my_json_type' if defined? ActiveRecord
5
+
6
+ module Blacksand
7
+ # inspired by rails-admin https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config.rb#L104
8
+ DEFAULT_AUTHENTICATION = proc {}
9
+ DEFAULT_AUTHORIZE = proc {}
10
+ DEFAULT_CURRENT_METHOD = proc {}
11
+
12
+ def self.table_name_prefix
13
+ end
14
+
15
+ mattr_accessor :site_id
16
+ mattr_accessor :site_name
17
+ mattr_accessor :root_path
18
+
19
+ mattr_accessor :carrierwave_storage
20
+ mattr_accessor :carrierwave_store_dir_prefix
21
+
22
+ mattr_accessor :page_caching
23
+
24
+ self.carrierwave_storage = :file
25
+ self.carrierwave_store_dir_prefix = 'uploads'
26
+ self.page_caching = false
27
+
28
+ def self.authenticate_with(&block)
29
+ @authenticate = block if block
30
+ @authenticate || DEFAULT_AUTHENTICATION
31
+ end
32
+
33
+ def self.current_user_method(&block)
34
+ @current_user = block if block
35
+ @current_user || DEFAULT_CURRENT_METHOD
36
+ end
37
+
38
+ def self.authorize_with(*args)
39
+ extension = args.shift
40
+ # 目前只支持 cancancan
41
+ if extension.present? && extension == :cancancan
42
+ @authorize = proc do
43
+ @authorization_adapter = Blacksand::Cancancan.new(self)
44
+ end
45
+
46
+ elsif extension.present? && extension != :cancancan
47
+ puts "Error: Authorization only supports cancancan"
48
+ end
49
+
50
+ @authorize || DEFAULT_AUTHORIZE
51
+ end
52
+
53
+ end
@@ -0,0 +1,30 @@
1
+ require 'rails/generators'
2
+
3
+ module Blacksand
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+
7
+ def install
8
+ create_file("config/initializers/blacksand.rb", <<-RUBY)
9
+ Blacksand.site_id = 'site id'
10
+ Blacksand.site_name = 'site name'
11
+ Blacksand.root_path = 'root path'
12
+
13
+ # Setup authentication to be run as a before filter
14
+ # @example Devise admin
15
+ # Blacksand.authenticate_with do
16
+ # authenticate_admin!
17
+ # end
18
+
19
+ # Blacksand.carrierwave_storage = :file
20
+ # Blacksand.carrierwave_store_dir_prefix = 'uploads' # NOTICE: Kindeditor need config by their api
21
+
22
+ # Page caching
23
+ # Blacksand.page_caching = false
24
+ RUBY
25
+
26
+ rake 'railties:install:migrations'
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,28 @@
1
+ require 'rails/generators'
2
+
3
+ module Blacksand
4
+ module Generators
5
+ class NewSiteGenerator < Rails::Generators::NamedBase
6
+
7
+ def new_site
8
+ puts "Create Site: #{name}"
9
+ # site yaml
10
+ create_file("db/sites/#{name}.yml", <<-YAML)
11
+ # 模板
12
+ templates:
13
+ # - your template
14
+
15
+ # 原型
16
+ prototypes:
17
+ # - your prototype
18
+ YAML
19
+
20
+ # site theme
21
+ if yes?("Install theme #{name} ?(Y/n)")
22
+ generate 'themes_on_rails:theme', name
23
+ end
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,72 @@
1
+ # desc "Explaining what the task does"
2
+ # task :blacksand do
3
+ # # Task goes here
4
+ # end
5
+
6
+ namespace :blacksand do
7
+ desc "Clean unsued kindeditor assets"
8
+ task :clean_kindeditor_assets => :environment do ||
9
+ puts "Delete unsued kindeditor assets 1 day ago"
10
+ assets = Kindeditor::Asset.where(owner_id: 0).where("created_at <= ?", 1.day.ago)
11
+ puts "Assets: #{assets.count}"
12
+ assets.destroy_all
13
+ end
14
+
15
+ desc "Seed templates and prototypes and clean unused of them"
16
+ task :seed, [:site_id] => :environment do |t, args|
17
+ site_id = args.site_id
18
+
19
+ data = YAML.load_file(Rails.root.join("db/sites/#{site_id}.yml")).deep_symbolize_keys
20
+
21
+ data[:templates].each do |template|
22
+ t = Blacksand::Template.where(name: template[:name]).first_or_initialize(path: template[:path], options: template[:options])
23
+ if t.new_record?
24
+ puts "Create Template #{t.name}"
25
+ t.save!
26
+ else
27
+ t.update_attributes!(path: template[:path], options: template[:options])
28
+ end
29
+ end
30
+
31
+ # 删除遗留的模板
32
+ Blacksand::Template.where('name not in (?)', data[:templates].map { |t| t[:name] }).each do |t|
33
+ puts "Delete template: #{t.name}"
34
+ t.destroy!
35
+ end
36
+
37
+ data[:prototypes].each do |prototype|
38
+ params = ActionController::Parameters.new(prototype: prototype)
39
+ prototype_params = params.require(:prototype).permit! #(:name, fields_attributes: [:name, :field_type, :description, :required, :options => []])
40
+
41
+ p = Blacksand::Prototype.where(name: prototype[:name]).first_or_initialize(prototype_params)
42
+ if p.new_record?
43
+ puts "Create Prototype #{p.name}"
44
+ p.save!
45
+ else
46
+ p.update!(options: prototype_params[:options])
47
+ # seed 一次换一次 fields, 但是之前关联 field 的地方都需要变
48
+ prototype[:fields_attributes].each do |field|
49
+ field_attributes = field.slice(:field_type, :description, :required, :options)
50
+ f = p.fields.where(name: field[:name]).first_or_initialize(field_attributes)
51
+ if f.new_record?
52
+ puts "Create Field #{p.name} / #{f.name}"
53
+ f.save!
54
+ else
55
+ f.update_attributes!(field_attributes)
56
+ end
57
+ end
58
+
59
+ # 删除多余的 field
60
+ to_deleted_fields = p.fields.map(&:name) - prototype[:fields_attributes].map { |f| f[:name] }
61
+ puts "Delete Fields #{p.name} / #{to_deleted_fields}" if to_deleted_fields.any?
62
+ p.fields.where(name: to_deleted_fields).destroy_all
63
+ end
64
+ end
65
+
66
+ # 删除遗留的原型
67
+ Blacksand::Prototype.where('name not in (?)', data[:prototypes].map { |p| p[:name] }).each do |p|
68
+ puts "Delete prototype: #{p.name}"
69
+ p.destroy!
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,25 @@
1
+ namespace :carrierwave do
2
+ desc "Migrate to new storage from file"
3
+ task :from_file_to_qiniu, [:model_class, :attribute] => :environment do |t, args|
4
+ model_class, attribute = args.model_class, args.attribute
5
+ model_class.constantize.all.each do |model|
6
+ puts "migrate: #{model_class} #{model.id}"
7
+ if model.try(attribute).present?
8
+ attr = model.try(attribute)
9
+ filename = File.basename(attr.path)
10
+ # 使用原有的 store_dir 找到本地文件,如果 store_dir 逻辑没有改的话。
11
+ file = Rails.root.join('public', attr.store_dir, filename)
12
+ puts "Upload #{file}"
13
+ File.open(file) do |f|
14
+ model.public_send("#{attribute}=", f)
15
+ end
16
+ model.save!
17
+ end
18
+ end
19
+ end
20
+
21
+ # TODO: 独立出 public/uploads 目录, 和 store_dir 策略
22
+ # def store_dir(model, attribute)
23
+ # "uploads/#{model.class.to_s.underscore}/#{attribute}/#{model.id}"
24
+ # end
25
+ end