cfror 0.0.6 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/models/cfror/field.rb +43 -0
- data/app/views/layouts/cfror/_field.html.erb +32 -0
- data/config/locales/cfror_ru.yml +1 -1
- data/lib/cfror/version.rb +1 -1
- data/lib/cfror.rb +60 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4eefe56bb8a732fb69ef552fe2c16f7cef3c2e71
|
4
|
+
data.tar.gz: ff209282e7ba4f4da20da47edfa82e334df3e63d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: adf9636bbfa34997535afe409054858b939f6f479e7710a00a45b20b5ebf6b80d54a4922f5d03428091e1723dfb9f59fadc6f985d3dfe76badd89d2cb35210dd
|
7
|
+
data.tar.gz: 3e2b8fc12b4c3893ec7253c98f367bb72b8867f2184a864b271e36da7732017b0c3203712914aa6f554c51ba156f4a00d24083c1dc2a6969124c87bbed555ff8
|
data/app/models/cfror/field.rb
CHANGED
@@ -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
|
+
|
data/config/locales/cfror_ru.yml
CHANGED
data/lib/cfror/version.rb
CHANGED
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.
|
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-
|
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
|