sunrise-cms 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -6,8 +6,7 @@ Open source mini content management system for programmers.
6
6
 
7
7
  Create new project
8
8
 
9
- rails new [PROJECT_NAME] -d mysql -T -m https://gist.github.com/1523718.txt
10
-
9
+ rails new [PROJECT_NAME] -d mysql -T -m https://raw.github.com/gist/2700424
11
10
 
12
11
  == Export data
13
12
 
@@ -28,4 +27,53 @@ For more info look at jbuilder https://rubygems.org/gems/jbuilder.
28
27
 
29
28
  GET /manage/users/export.xlsx
30
29
 
30
+ == Usage
31
+
32
+ Just create class:
33
+
34
+ class SunriseProduct < Sunrise::AbstractModel
35
+ self.resource_name = "Product"
36
+
37
+ list :thumbs do
38
+ scope { Product.includes(:picture) }
39
+ preview { lambda { |product| product.picture.try(:url, :thumb) } }
40
+
41
+ field :title
42
+ field :price
43
+ field :total_stock
44
+ end
45
+
46
+ show do
47
+ field :title
48
+ field :price
49
+ field :total_stock
50
+ field :sort_order
51
+ field :is_visible
52
+ end
53
+
54
+ edit do
55
+ field :title
56
+ field :price
57
+ field :total_stock
58
+ field :notes
59
+
60
+ group :sidebar, :holder => :sidebar do
61
+ field :sale_limit_id, :collection => lambda { SaleLimit.all }, :include_blank => false
62
+ field :sort_order
63
+ field :is_visible, :boolean => true
64
+ end
65
+
66
+ group :bottom, :holder => :bottom do
67
+ nested_attributes :variants do
68
+ field :size
69
+ field :total_stock
70
+ field :item_model_id, :collection => lambda { ItemModel.all }, :include_blank => false
71
+ end
72
+
73
+ field :picture, :as => :uploader
74
+ end
75
+ end
76
+ end
77
+
78
+
31
79
  Copyright (c) 2012 Fodojo, released under the MIT license
@@ -136,6 +136,24 @@ class Sunrise
136
136
  else
137
137
  return ret
138
138
 
139
+ insert_fields: (link, method, content) ->
140
+ new_id = new Date().getTime();
141
+ regexp = new RegExp("new_" + method, "g")
142
+
143
+ console.log link
144
+
145
+ $(link).parents("div.nested_bottom").before(content.replace(regexp, new_id))
146
+
147
+ remove_fields: (link) ->
148
+ hidden_field = $(link).prev("input[type=hidden]")
149
+
150
+ if hidden_field.length isnt 0
151
+ hidden_field.val('1')
152
+
153
+ console.log link
154
+
155
+ $(link).closest("div.nested_item").hide()
156
+
139
157
  $(document).ready ->
140
158
  window['sunrise'] ?= new Sunrise(window.location.pathname)
141
159
  window['sunrise'].setup()
@@ -57,3 +57,46 @@ input[type="text"]:focus, input[type="password"]:focus, input[type="email"]:focu
57
57
  .treeHolder li.ui-sortable-helper {
58
58
  background: none;
59
59
  }
60
+
61
+ /* Nested form */
62
+ .nested {
63
+ overflow: hidden;
64
+ background-color: #f5f5f5;
65
+ border: 1px solid #dbdbdb;
66
+ border-radius: 2px;
67
+ -moz-border-radius: 2px;
68
+ -webkit-box-shadow: 0px 0px 1px 1px #fff;
69
+ -moz-box-shadow: 0px 0px 1px 1px #fff;
70
+ box-shadow: 0px 0px 1px 1px #fff;
71
+ padding: 15px 0;
72
+ margin-bottom: 20px;
73
+ }
74
+ .nested .nested_item {
75
+ border-top: 1px solid #DBDBDB;
76
+ border-bottom: 1px solid #DBDBDB;
77
+ margin: 5px 0 -6px 0;
78
+ box-shadow: 0 0 1px 1px #FFFFFF;
79
+ padding: 15px 0 0px;
80
+ position: relative;
81
+ overflow: hidden;
82
+ }
83
+ .nested .nested_bottom {
84
+ margin-left: 20px;
85
+ margin-right: 20px;
86
+ margin-top: 20px;
87
+ clear: both;
88
+ }
89
+ .nested .nested_bottom .button {
90
+ display: inline-block;
91
+ }
92
+ .nested .padder {
93
+ margin-left: 20px;
94
+ margin-right: 20px;
95
+ float:left;
96
+ width:200px;
97
+ clear: none;
98
+ }
99
+ .nested .title-switcher {
100
+ float:none;
101
+ }
102
+ /* End nested form */
@@ -6,10 +6,11 @@ module Sunrise
6
6
 
7
7
  def index
8
8
  per_page = Sunrise::Config.audit_events_per_page
9
+ sort_mode = Sunrise::Config.default_sort_mode
9
10
  cur_page = (params[:page] || 1).to_i
10
11
  offset = (cur_page - 1) * per_page
11
12
 
12
- @events = Audited.audit_class.includes(:user).limit(per_page).offset(offset)
13
+ @events = Audited.audit_class.includes(:user).limit(per_page).offset(offset).order("audits.id #{sort_mode}")
13
14
 
14
15
  respond_with(@events) do |format|
15
16
  format.html { render :layout => params[:time].blank? }
@@ -31,5 +31,35 @@ module Sunrise
31
31
  item.to_s
32
32
  end
33
33
  end
34
+
35
+ def manage_remove_child_link(name, form, options = {})
36
+ options[:onclick] = h("sunrise.remove_fields(this);")
37
+ form.hidden_field(:_destroy) + link_to(name, "javascript:void(0);", options)
38
+ end
39
+
40
+ def manage_add_child_link(name, form, field, options={})
41
+ options.symbolize_keys!
42
+
43
+ method = field.name.to_sym
44
+ html_options = (options.delete(:html) || {})
45
+ fields = manage_new_child_fields(form, field, options)
46
+
47
+ html_options[:class] ||= "button"
48
+ html_options[:onclick] = h("sunrise.insert_fields(this, \"#{method}\", \"#{escape_javascript(fields)}\");")
49
+
50
+ link_to(name, "javascript:void(0);", html_options)
51
+ end
52
+
53
+ def manage_new_child_fields(form_builder, field, options = {})
54
+ method = field.name.to_sym
55
+
56
+ options[:object] ||= form_builder.object.class.reflect_on_association(method).klass.new
57
+ options[:partial] ||= method.to_s.singularize
58
+ options[:form_builder_local] ||= :form
59
+
60
+ form_builder.fields_for(method, options[:object], :child_index => "new_#{method}") do |f|
61
+ render(:partial => options[:partial], :locals => { options[:form_builder_local] => f, :field => field })
62
+ end
63
+ end
34
64
  end
35
65
  end
@@ -1,7 +1,21 @@
1
1
  <% options ||= {} %>
2
2
  <%= content_tag :div, field.html_options do %>
3
3
  <% if field.visible?(form.object) -%>
4
- <% if field.association? -%>
4
+ <% if field.nested? -%>
5
+ <div class="nested">
6
+ <div class="title-switcher up"><%= form.label field.name %></div>
7
+
8
+ <%= form.simple_fields_for field.name.to_sym do |nested| %>
9
+ <%= render :partial => "nested_field", :locals => { :form => nested, :field => field } %>
10
+ <% end -%>
11
+
12
+ <% if field.multiply? %>
13
+ <div class="nested_bottom">
14
+ <%= manage_add_child_link t('manage.buttons.add_nested_field'), form, field, {:partial=>"nested_field"} %>
15
+ </div>
16
+ <% end -%>
17
+ </div>
18
+ <% elsif field.association? -%>
5
19
  <%= form.association field.name.to_sym, field.input_options.merge(options) %>
6
20
  <% else -%>
7
21
  <%= form.input field.name, field.input_options.merge(options) %>
@@ -0,0 +1,4 @@
1
+ <div class="nested_item">
2
+ <%= manage_remove_child_link '', form, :class => "close-but" %>
3
+ <%= render :partial => 'field', :collection => field.fields, :locals => {:form => form} %>
4
+ </div>
@@ -140,6 +140,7 @@ en:
140
140
 
141
141
  buttons:
142
142
  create_structure: "Create Section"
143
+ add_nested_field: "+Add"
143
144
 
144
145
  user:
145
146
  actions: "action"
@@ -158,6 +158,7 @@ ru:
158
158
  thumbs: "Список"
159
159
  edit_profile: "Мой профайл"
160
160
  exit: "Выход"
161
+ add_nested_field: "+Еще"
161
162
 
162
163
  user:
163
164
  actions: "действия"
@@ -158,6 +158,7 @@ uk:
158
158
  thumbs: "Список"
159
159
  edit_profile: "Мій профайл"
160
160
  exit: "Вихід"
161
+ add_nested_field: "+Ще"
161
162
 
162
163
  user:
163
164
  actions: "дії"
@@ -151,14 +151,11 @@ module Sunrise
151
151
  model.update_all("#{@sort_column} = CASE id #{sql_case}", ["id IN (?)", ids.keys.map(&:to_i)])
152
152
  end
153
153
 
154
- # Initialize new model and set parent record
154
+ # Initialize new model, sets parent record and call build_defaults method
155
155
  def build_record
156
156
  record = model.new
157
-
158
- if parent_record
159
- record.send("#{parent_association.name}=", parent_record)
160
- end
161
-
157
+ record.send("#{parent_association.name}=", parent_record) if parent_record
158
+ record.build_defaults if record.respond_to?(:build_defaults)
162
159
  record
163
160
  end
164
161
 
@@ -4,7 +4,7 @@ module Sunrise
4
4
  module Config
5
5
  class Field < Base
6
6
  include Sunrise::Utils::EvalHelpers
7
-
7
+
8
8
  # The condition that must be met on an object
9
9
  attr_reader :if_condition
10
10
 
@@ -46,6 +46,10 @@ module Sunrise
46
46
  def label?
47
47
  @config_options[:label] != false
48
48
  end
49
+
50
+ def nested?
51
+ false
52
+ end
49
53
 
50
54
  protected
51
55
 
@@ -1,4 +1,5 @@
1
1
  require 'sunrise/config/field'
2
+ require 'sunrise/config/nested_field'
2
3
 
3
4
  module Sunrise
4
5
  module Config
@@ -15,6 +16,14 @@ module Sunrise
15
16
  options = { :name => name.to_sym }.merge(options)
16
17
  fields << Field.new(abstract_model, self, options)
17
18
  end
19
+
20
+ # Defines a configuration for a nested attributes
21
+ def nested_attributes(name, options = {}, &block)
22
+ options = { :name => name.to_sym }.merge(options)
23
+ nested_field = NestedField.new(abstract_model, self, options)
24
+ nested_field.instance_eval &block if block
25
+ fields << nested_field
26
+ end
18
27
  end
19
28
  end
20
29
  end
@@ -0,0 +1,27 @@
1
+ require 'sunrise/config/field'
2
+
3
+ module Sunrise
4
+ module Config
5
+ class NestedField < Field
6
+
7
+ # Array for store all defined fields
8
+ def fields
9
+ @fields ||= []
10
+ end
11
+
12
+ # Defines a configuration for a field.
13
+ def field(name, options = {})
14
+ options = { :name => name.to_sym }.merge(options)
15
+ fields << Field.new(abstract_model, self, options)
16
+ end
17
+
18
+ def nested?
19
+ true
20
+ end
21
+
22
+ def multiply?
23
+ @config_options[:multiply] != false
24
+ end
25
+ end
26
+ end
27
+ end
@@ -12,7 +12,7 @@ module Sunrise
12
12
 
13
13
  # By default show latest first
14
14
  mattr_accessor :default_sort_mode
15
- @@default_sort_reverse = :desc
15
+ @@default_sort_mode = :desc
16
16
 
17
17
  # The display for a model instance (i.e. a single database record).
18
18
  mattr_accessor :label_methods
@@ -130,6 +130,10 @@ module Sunrise
130
130
  self[key] = value
131
131
  end
132
132
  end
133
+
134
+ def to_key
135
+ ['settings']
136
+ end
133
137
  end
134
138
 
135
139
  #get the value field, YAML decoded
@@ -1,3 +1,3 @@
1
1
  module Sunrise
2
- VERSION = "0.4.1".freeze
2
+ VERSION = "0.4.2".freeze
3
3
  end