cfror 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eb0e002bc8d7107691c202e1c5d7e8e31c4ba256
4
- data.tar.gz: c5696e0d9bafdac102df6bbfa2d4866587261f67
3
+ metadata.gz: 4eefe56bb8a732fb69ef552fe2c16f7cef3c2e71
4
+ data.tar.gz: ff209282e7ba4f4da20da47edfa82e334df3e63d
5
5
  SHA512:
6
- metadata.gz: 183f71d6d6145dd374426abeece51a71eede6668e74dfb5481e15f2ed877b202539856bcc0be84c916ed36a30fe09a13c8060a617de3ec840b56d07361877654
7
- data.tar.gz: 4aa5e842191f58512a9ed157f3fa9513511af071149b1d505d3213c7f27798e7fb62a7c4413d058ee8bac74b476cd26d6e66d4350606105822775361b6b229e0
6
+ metadata.gz: adf9636bbfa34997535afe409054858b939f6f479e7710a00a45b20b5ebf6b80d54a4922f5d03428091e1723dfb9f59fadc6f985d3dfe76badd89d2cb35210dd
7
+ data.tar.gz: 3e2b8fc12b4c3893ec7253c98f367bb72b8867f2184a864b271e36da7732017b0c3203712914aa6f554c51ba156f4a00d24083c1dc2a6969124c87bbed555ff8
@@ -2,6 +2,8 @@ module Cfror
2
2
  class Field < ActiveRecord::Base
3
3
  enum field_type: [ :string, :text, :integer, :boolean, :date, :option, :image, :datetime ]
4
4
 
5
+ attr_accessor :value_object
6
+
5
7
  belongs_to :fieldable, polymorphic: true
6
8
 
7
9
  has_many :select_options, dependent: :destroy
@@ -15,5 +17,46 @@ module Cfror
15
17
  has_many :datetimes, dependent: :destroy
16
18
  has_many :options, dependent: :destroy
17
19
  has_many :images, dependent: :destroy
20
+
21
+
22
+ def save_value!(model, value)
23
+ type = field_type.to_sym
24
+
25
+ if type == :option
26
+ save_option!(model, value)
27
+ else
28
+ data = send("#{field_type}s").find_by(dataable: model)
29
+ if data
30
+ data.update_attribute(:body, value)
31
+ else
32
+ send("#{field_type}s").create!(dataable: model, body: value)
33
+ end
34
+ end
35
+ end
36
+
37
+ def save_option!(model, value)
38
+ option_data = self.options.find_by(dataable: model)
39
+ if option_data
40
+ option_data.update_attribute(:select_option, SelectOption.find(value))
41
+ else
42
+ options.create!(dataable: model, select_option: SelectOption.find(value))
43
+ end
44
+ end
45
+
46
+ def set_value_for(model)
47
+ type = field_type.to_sym
48
+
49
+ self.value_object = if type == :option
50
+ self.options.find_by(dataable: model).try(:select_option)
51
+ else
52
+ send("#{field_type}s").find_by(dataable: model)
53
+ end
54
+ end
55
+
56
+ def value
57
+ self.value_object.try(:body)
58
+ end
59
+
60
+
18
61
  end
19
62
  end
@@ -0,0 +1,32 @@
1
+ <label>
2
+ <%= field.title %>
3
+ <br/>
4
+ <%- case field.field_type
5
+ when 'integer' %>
6
+ <input type="number" name="cfror_fields[<%= field.id %>]" value="<%= field.integers.find_by(dataable: model).try(:body) %>" id="cfror_fields_<%=field.id%>" class="cfror_number_input"/>
7
+ <%- when 'string' %>
8
+ <input type="text" name="cfror_fields[<%= field.id %>]" value="<%= field.strings.find_by(dataable: model).try(:body) %>" id="cfror_fields_<%=field.id%>" class="cfror_text_input"/>
9
+ <%- when 'boolean' %>
10
+ <input type="checkbox" name="cfror_fields[<%= field.id %>]" value="1" <%= field.booleans.find_by(dataable: model).try(:body) ? "checked" : '' %> id="cfror_fields_<%=field.id%>" class="cfror_checkbox_input"/>
11
+ <%- when 'text' %>
12
+ <textarea name="cfror_fields[<%= field.id %>]" class="cfror_textarea" id="cfror_fields_<%=field.id%>"><%= field.texts.find_by(dataable: model).try(:body) %></textarea>
13
+ <%- when 'date' %>
14
+ <input type="date" name="cfror_fields[<%= field.id %>]" value="<%= field.dates.find_by(dataable: model).try(:body) %>" id="cfror_fields_<%=field.id%>" class="cfror_date_input"/>
15
+ <%- when 'datetime' %>
16
+ <input type="date" name="cfror_fields[<%= field.id %>]" value="<%= field.datetimes.find_by(dataable: model).try(:body) %>" id="cfror_fields_<%=field.id%>" class="cfror_datetime_input"/>
17
+ <%- when 'image' %>
18
+ <input type="file" name="cfror_fields[<%= field.id %>]" value="<%= field.images.find_by(dataable: model).try(:body) %>" id="cfror_fields_<%=field.id%>" class="cfror_file_input"/>
19
+ <%- when 'option' %>
20
+ <%- value_option = field.options.find_by(dataable: model).try(:select_option_id) %>
21
+ <select name="cfror_fields[<%= field.id %>" id="cfror_fields_<%=field.id%>" class="cfror_select">
22
+ <% field.select_options.order(:id).each do |option| %>
23
+ <% if value_option and option.id.to_i == value_option.to_i %>
24
+ <option value="<%= option.id %>" selected='selected'><%= option.body %></option>
25
+ <% else %>
26
+ <option value="<%= option.id %>"><%= option.body %></option>
27
+ <% end %>
28
+ <% end %>
29
+ </select>
30
+ <% end %>
31
+ </label>
32
+
@@ -6,6 +6,6 @@ ru:
6
6
  integer: Число
7
7
  boolean: 'Да/Нет'
8
8
  date: Дата
9
- datetimes: Дата и время
9
+ datetime: Дата и время
10
10
  option: Список
11
11
  image: Изображение
data/lib/cfror/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Cfror
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/cfror.rb CHANGED
@@ -1,4 +1,64 @@
1
1
  require "cfror/engine"
2
2
 
3
3
  module Cfror
4
+ #Including to fields model
5
+ module Fields
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ has_many :fields, as: :fieldable, class_name: Cfror::Field, dependent: :destroy
10
+ accepts_nested_attributes_for :fields, allow_destroy: true, reject_if: :all_blank
11
+ end
12
+ end
13
+
14
+ #Including to data model
15
+ module Data
16
+ extend ActiveSupport::Concern
17
+
18
+ included do
19
+ has_many :boolean_datums, as: :dataable, class_name: Cfror::Boolean, dependent: :destroy
20
+ #accepts_nested_attributes_for :boolean_datums, allow_destroy: true, reject_if: :all_blank
21
+
22
+ has_many :date_datums, as: :dataable, class_name: Cfror::Date, dependent: :destroy
23
+ #accepts_nested_attributes_for :date_datums, allow_destroy: true, reject_if: :all_blank
24
+
25
+ has_many :datetime_datums, as: :dataable, class_name: Cfror::Datetime, dependent: :destroy
26
+ #accepts_nested_attributes_for :datetime_datums, allow_destroy: true, reject_if: :all_blank
27
+
28
+ has_many :image_datums, as: :dataable, class_name: Cfror::Image, dependent: :destroy
29
+ #accepts_nested_attributes_for :image_datums, allow_destroy: true, reject_if: :all_blank
30
+
31
+ has_many :integer_datums, as: :dataable, class_name: Cfror::Integer, dependent: :destroy
32
+ #accepts_nested_attributes_for :image_datums, allow_destroy: true, reject_if: :all_blank
33
+
34
+ has_many :option_datums, as: :dataable, class_name: Cfror::Option, dependent: :destroy
35
+ #accepts_nested_attributes_for :option_datums, allow_destroy: true, reject_if: :all_blank
36
+
37
+ has_many :string_datums, as: :dataable, class_name: Cfror::String, dependent: :destroy
38
+ #accepts_nested_attributes_for :string_datums, allow_destroy: true, reject_if: :all_blank
39
+
40
+ has_many :text_datums, as: :dataable, class_name: Cfror::Text, dependent: :destroy
41
+ #accepts_nested_attributes_for :text_datums, allow_destroy: true, reject_if: :all_blank
42
+ end
43
+
44
+ #save fields value
45
+ def save_cfror_fields(fields)
46
+ fields.each do |field, value|
47
+ field = Cfror::Field.find(field)
48
+ field.save_value!(self, value)
49
+ end
50
+ end
51
+
52
+ #set values for fields
53
+ #@param source is symbol of relation method contains include Cfror::Fields
54
+ def value_fields_for(source)
55
+ fields = self.send(source).fields
56
+
57
+ fields.each do |i|
58
+ i.set_value_for(self)
59
+ end
60
+
61
+ fields
62
+ end
63
+ end
4
64
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfror
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Dmitriev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-10 00:00:00.000000000 Z
11
+ date: 2015-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -61,6 +61,7 @@ files:
61
61
  - app/models/cfror/string.rb
62
62
  - app/models/cfror/text.rb
63
63
  - app/uploaders/cfror/image_uploader.rb
64
+ - app/views/layouts/cfror/_field.html.erb
64
65
  - app/views/layouts/cfror/application.html.erb
65
66
  - config/locales/cfror_en.yml
66
67
  - config/locales/cfror_ru.yml