binda 0.0.6 → 0.0.7

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.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -2
  3. data/Rakefile +2 -1
  4. data/app/assets/javascripts/binda/dist/binda.bundle.js +75 -75
  5. data/app/assets/stylesheets/binda/settings/common.scss +4 -0
  6. data/app/controllers/binda/application_controller.rb +1 -1
  7. data/app/controllers/binda/boards_controller.rb +73 -0
  8. data/app/controllers/binda/components_controller.rb +18 -44
  9. data/app/controllers/binda/field_groups_controller.rb +36 -24
  10. data/app/controllers/binda/field_settings_controller.rb +5 -5
  11. data/app/controllers/binda/manage/users_controller.rb +2 -9
  12. data/app/controllers/binda/structures_controller.rb +6 -13
  13. data/app/controllers/concerns/binda/default_helpers.rb +226 -0
  14. data/app/controllers/concerns/binda/fieldable_helpers.rb +44 -0
  15. data/app/helpers/binda/boards_helper.rb +13 -0
  16. data/app/helpers/binda/components_helper.rb +0 -5
  17. data/app/helpers/binda/field_settings_helper.rb +1 -1
  18. data/app/models/binda/asset.rb +0 -8
  19. data/app/models/binda/binda.rb +7 -0
  20. data/app/models/binda/binding.rb +4 -4
  21. data/app/models/binda/board.rb +41 -0
  22. data/app/models/binda/category.rb +7 -3
  23. data/app/models/binda/checkbox.rb +1 -3
  24. data/app/models/binda/choice.rb +31 -1
  25. data/app/models/binda/component.rb +7 -22
  26. data/app/models/binda/field_group.rb +13 -7
  27. data/app/models/binda/field_setting.rb +40 -29
  28. data/app/models/binda/gallery.rb +0 -7
  29. data/app/models/binda/radio.rb +1 -1
  30. data/app/models/binda/repeater.rb +13 -20
  31. data/app/models/binda/selection.rb +17 -0
  32. data/app/models/binda/string.rb +4 -0
  33. data/app/models/binda/structure.rb +65 -7
  34. data/app/models/concerns/binda/{component_model_helper.rb → fieldable_associations.rb} +102 -31
  35. data/app/views/binda/boards/edit.html.erb +7 -0
  36. data/app/views/binda/components/edit.html.erb +4 -1
  37. data/app/views/binda/components/index.html.erb +2 -0
  38. data/app/views/binda/components/new.html.erb +1 -1
  39. data/app/views/binda/field_groups/_form_body.html.erb +8 -8
  40. data/app/views/binda/field_groups/_form_item.html.erb +3 -3
  41. data/app/views/binda/field_groups/_form_item_choice.erb +11 -8
  42. data/app/views/binda/field_groups/_form_section.html.erb +5 -5
  43. data/app/views/binda/field_groups/_form_section_repeater.html.erb +3 -3
  44. data/app/views/binda/field_groups/edit.html.erb +1 -1
  45. data/app/views/binda/field_groups/new.html.erb +1 -1
  46. data/app/views/binda/{components → fieldable}/_form_body.html.erb +9 -9
  47. data/app/views/binda/{components → fieldable}/_form_item_asset.html.erb +0 -0
  48. data/app/views/binda/{components → fieldable}/_form_item_date.html.erb +0 -0
  49. data/app/views/binda/{components → fieldable}/_form_item_gallery.html.erb +0 -0
  50. data/app/views/binda/{components → fieldable}/_form_item_new_repeater.html.erb +5 -5
  51. data/app/views/binda/{components → fieldable}/_form_item_repeater.html.erb +8 -9
  52. data/app/views/binda/fieldable/_form_item_selections.html.erb +99 -0
  53. data/app/views/binda/fieldable/_form_item_string.html.erb +19 -0
  54. data/app/views/binda/{components → fieldable}/_form_item_text.html.erb +2 -11
  55. data/app/views/binda/fieldable/_form_section.html.erb +52 -0
  56. data/app/views/binda/fieldable/_form_section_repeater.html.erb +48 -0
  57. data/app/views/binda/fieldable/_form_sidebar.html.erb +35 -0
  58. data/app/views/binda/structures/_form_body.html.erb +6 -0
  59. data/app/views/binda/structures/_form_section.html.erb +1 -1
  60. data/app/views/binda/structures/edit.html.erb +3 -0
  61. data/app/views/binda/structures/index.html.erb +2 -0
  62. data/app/views/layouts/binda/_flash.html.erb +3 -3
  63. data/app/views/layouts/binda/_sidebar.html.erb +20 -15
  64. data/config/locales/en.yml +4 -0
  65. data/config/routes.rb +18 -21
  66. data/db/migrate/1_create_binda_tables.rb +12 -6
  67. data/lib/binda/engine.rb +5 -1
  68. data/lib/binda/version.rb +1 -1
  69. data/lib/generators/binda/install/install_generator.rb +9 -0
  70. data/lib/generators/binda/setup/setup_generator.rb +50 -7
  71. data/lib/tasks/binda.rake +78 -24
  72. metadata +100 -31
  73. data/app/controllers/binda/settings_controller.rb +0 -75
  74. data/app/controllers/concerns/binda/component_controller_helper.rb +0 -16
  75. data/app/helpers/binda/settings_helper.rb +0 -13
  76. data/app/models/binda/select.rb +0 -7
  77. data/app/models/binda/setting.rb +0 -42
  78. data/app/views/binda/components/_form_item_selectable.html.erb +0 -72
  79. data/app/views/binda/components/_form_section.html.erb +0 -42
  80. data/app/views/binda/components/_form_section_repeater.html.erb +0 -41
  81. data/app/views/binda/components/_form_sidebar.html.erb +0 -34
  82. data/app/views/binda/settings/_dashboard_form.html.erb +0 -51
  83. data/app/views/binda/settings/_form.html.erb +0 -52
  84. data/app/views/binda/settings/dashboard.html.erb +0 -9
  85. data/app/views/binda/settings/edit.html.erb +0 -8
  86. data/app/views/binda/settings/index.html.erb +0 -34
  87. data/app/views/binda/settings/new.html.erb +0 -8
@@ -0,0 +1,13 @@
1
+ module Binda
2
+ module BoardsHelper
3
+
4
+ def get_website_name
5
+ Board.friendly.find('dashboard').get_string('website-name')
6
+ end
7
+
8
+ def get_website_description
9
+ Board.friendly.find('dashboard').get_text('website-description')
10
+ end
11
+
12
+ end
13
+ end
@@ -1,10 +1,5 @@
1
1
  module Binda
2
2
  module ComponentsHelper
3
3
 
4
- def get_form_component_url
5
- return structure_components_path if action_name == 'new'
6
- return structure_component_path if action_name == 'edit'
7
- end
8
-
9
4
  end
10
5
  end
@@ -7,7 +7,7 @@ module Binda
7
7
  end
8
8
 
9
9
  def get_field_types
10
- FieldSetting.new.get_field_types
10
+ FieldSetting.get_field_classes.map{ |fc| fc.to_s.underscore }
11
11
  end
12
12
 
13
13
  end
@@ -9,13 +9,5 @@ module Binda
9
9
 
10
10
  mount_uploader :image, ImageUploader
11
11
 
12
- # Validations
13
-
14
- # Slug
15
-
16
-
17
- # CUSTOM METHODS
18
- # --------------
19
-
20
12
  end
21
13
  end
@@ -0,0 +1,7 @@
1
+ module Binda
2
+ module Binda
3
+ def self.table_name_prefix
4
+ 'binda_binda_'
5
+ end
6
+ end
7
+ end
@@ -12,10 +12,10 @@ module Binda
12
12
  extend FriendlyId
13
13
  friendly_id :name, use: [:slugged, :finders]
14
14
 
15
-
16
- # CUSTOM METHODS
17
- # --------------
18
- # https://github.com/norman/friendly_id/issues/436
15
+ # Friendly id preference on slug generation
16
+ #
17
+ # Method inherited from friendly id
18
+ # @see https://github.com/norman/friendly_id/issues/436
19
19
  def should_generate_new_friendly_id?
20
20
  slug.blank?
21
21
  end
@@ -0,0 +1,41 @@
1
+ module Binda
2
+ class Board < ApplicationRecord
3
+
4
+ include FieldableAssociations
5
+
6
+ belongs_to :structure, required: true
7
+
8
+ validates :name, presence: true
9
+ validates :slug, uniqueness: true
10
+
11
+ # Slug
12
+ extend FriendlyId
13
+ friendly_id :name, use: [:slugged, :finders]
14
+
15
+ # Friendly id preference on slug generation
16
+ #
17
+ # Method inherited from friendly id
18
+ # @see https://github.com/norman/friendly_id/issues/436
19
+ def should_generate_new_friendly_id?
20
+ slug.blank?
21
+ end
22
+
23
+ # def self.website_name
24
+ # self.all.find { |s| s.slug == 'website-name' }
25
+ # end
26
+
27
+ # def self.website_description
28
+ # self.all.find { |s| s.slug == 'website-description' }
29
+ # end
30
+
31
+ # def self.maintenance_mode
32
+ # self.all.find { |s| s.slug == 'maintenance-mode' }
33
+ # end
34
+
35
+ # def self.is_maintenance_mode
36
+ # # maybe this should have question mark at the end...
37
+ # self.maintenance_mode.is_true
38
+ # end
39
+
40
+ end
41
+ end
@@ -13,13 +13,17 @@ module Binda
13
13
  friendly_id :default_slug, use: [:slugged, :finders]
14
14
 
15
15
 
16
- # CUSTOM METHODS
17
- # --------------
18
- # https://github.com/norman/friendly_id/issues/436
16
+ # Friendly id preference on slug generation
17
+ #
18
+ # Method inherited from friendly id
19
+ # @see https://github.com/norman/friendly_id/issues/436
19
20
  def should_generate_new_friendly_id?
20
21
  slug.blank? || name_changed?
21
22
  end
22
23
 
24
+ # Set slug name
25
+ #
26
+ # It generates 4 possible slugs before falling back to FriendlyId default behaviour
23
27
  def default_slug
24
28
  [ "#{ self.structure.name }-#{ self.name }",
25
29
  "#{ self.structure.name }-#{ self.name }-1",
@@ -1,7 +1,5 @@
1
1
  module Binda
2
- class Checkbox < Select
3
-
4
- has_many :choices, as: :selectable
2
+ class Checkbox < Selection
5
3
 
6
4
  end
7
5
  end
@@ -2,10 +2,40 @@ module Binda
2
2
  class Choice < ApplicationRecord
3
3
 
4
4
  belongs_to :field_setting
5
- belongs_to :selectable, polymorphic: true, optional: true
5
+ has_and_belongs_to_many :selections
6
6
 
7
7
  validates :label, presence: true
8
8
  validates :value, presence: true
9
+ validates :field_setting_id, presence: true
10
+
11
+ after_create :set_default_choice
12
+ after_destroy :reset_default_choice
13
+
14
+ # In order to make sure that a default choice is set, this method is executed after
15
+ # the first choice is associated to the field setting. What does it do?
16
+ # If there isn't a default it sets the current choice as the default one.
17
+ # The method is executed as well if new choices are added.
18
+ def set_default_choice
19
+ unless self.field_setting.default_choice.present?
20
+ self.field_setting.default_choice_id = self.id
21
+ unless self.field_setting.save
22
+ raise ArgumentError "It hasn't been possible to set the default choice for the current setting."
23
+ end
24
+ end
25
+ end
26
+
27
+ # In order to make sure that a default choice is set, this method is executed after the
28
+ # default choice is removed. What does it do? If the removed choice is the default one it sets
29
+ # another defautl picking the first of the choices associated by id.
30
+ # The method is executed as well on every time a choice is removed.
31
+ def reset_default_choice
32
+ if self.field_setting.default_choice_id == self.id && self.field_setting.choices.any?
33
+ self.field_setting.default_choice_id = self.field_setting.choices.first.id
34
+ unless self.field_setting.save
35
+ raise ArgumentError "It hasn't been possible to set the default choice for the current setting."
36
+ end
37
+ end
38
+ end
9
39
 
10
40
  end
11
41
  end
@@ -1,34 +1,19 @@
1
1
  module Binda
2
2
  class Component < ApplicationRecord
3
3
 
4
- include ComponentModelHelper
4
+ include FieldableAssociations
5
5
 
6
6
  # Associations
7
7
  belongs_to :structure, required: true
8
8
  has_and_belongs_to_many :categories
9
- has_many :texts, as: :fieldable, dependent: :delete_all
10
- has_many :dates, as: :fieldable, dependent: :delete_all
11
- has_many :galleries, as: :fieldable, dependent: :delete_all
12
- has_many :assets, as: :fieldable, dependent: :delete_all
13
- has_many :radios, as: :fieldable, dependent: :delete_all
14
- has_many :selects, as: :fieldable, dependent: :delete_all
15
- has_many :checkboxes, as: :fieldable, dependent: :delete_all
16
- # Repeaters need destroy_all, not delete_all
17
- has_many :repeaters, as: :fieldable, dependent: :destroy
18
-
19
-
20
- accepts_nested_attributes_for :structure, :categories, :texts, :dates, :assets, :galleries, :repeaters, :radios, :selects, :checkboxes, allow_destroy: true
21
-
22
- cattr_accessor :field_settings_array
23
-
24
- # has_many :bindings
25
- # has_many :assets, class_name: 'Admin::Asset', through: :bindings
26
9
 
27
10
  # Validations
28
11
  validates :name, presence: true
29
12
  validates :slug, uniqueness: true
30
13
  validates :publish_state, presence: true, inclusion: { in: %w( draft published )}
31
14
 
15
+ accepts_nested_attributes_for :categories, allow_destroy: true
16
+
32
17
  # Slug
33
18
  extend FriendlyId
34
19
  friendly_id :name, use: [:slugged, :finders]
@@ -50,13 +35,13 @@ module Binda
50
35
 
51
36
  end
52
37
 
53
-
54
- # CUSTOM METHODS
55
- #
38
+ # Friendly id preference on slug generation
39
+ #
40
+ # Method inherited from friendly id
56
41
  # @see https://github.com/norman/friendly_id/issues/436
57
42
  def should_generate_new_friendly_id?
58
43
  slug.blank?
59
44
  end
60
-
45
+
61
46
  end
62
47
  end
@@ -19,13 +19,17 @@ module Binda
19
19
 
20
20
  after_create :update_position
21
21
 
22
- # CUSTOM METHODS
23
- # --------------
24
- # https://github.com/norman/friendly_id/issues/436
25
- def should_generate_new_friendly_id?
26
- slug.blank?
27
- end
28
-
22
+ # Friendly id preference on slug generation
23
+ #
24
+ # Method inherited from friendly id
25
+ # @see https://github.com/norman/friendly_id/issues/436
26
+ def should_generate_new_friendly_id?
27
+ slug.blank? || name_changed?
28
+ end
29
+
30
+ # Set slug name
31
+ #
32
+ # It generates 4 possible slugs before falling back to FriendlyId default behaviour
29
33
  def default_slug
30
34
  [ "#{ self.structure.name }-#{ self.name }",
31
35
  "#{ self.structure.name }-#{ self.name }-1",
@@ -33,6 +37,8 @@ module Binda
33
37
  "#{ self.structure.name }-#{ self.name }-3" ]
34
38
  end
35
39
 
40
+ #
41
+ # Sets the validation rules to accept and save an attribute
36
42
  def is_rejected( attributes )
37
43
  attributes['name'].blank? && attributes['field_type'].blank?
38
44
  end
@@ -8,41 +8,47 @@ module Binda
8
8
  # Fields Associations
9
9
  #
10
10
  # If you add a new field remember to update:
11
- # - get_fieldables (see here below)
12
- # - get_field_types (see here below)
11
+ # - get_field_classes (see here below)
13
12
  # - component_params (app/controllers/binda/components_controller.rb)
14
13
  has_many :texts, as: :fieldable
14
+ has_many :strings, as: :fieldable
15
15
  has_many :dates, as: :fieldable
16
16
  has_many :galleries, as: :fieldable
17
17
  has_many :assets, as: :fieldable
18
18
  has_many :repeater, as: :fieldable
19
19
  has_many :radio, as: :fieldable
20
- has_many :select, as: :fieldable
20
+ has_many :selection, as: :fieldable
21
21
  has_many :checkbox, as: :fieldable
22
22
 
23
23
 
24
24
  # The following direct association is used to securely delete associated fields
25
25
  # Infact via `fieldable` the associated fields might not be deleted
26
- # as the fieldable_id is related to the `component` rather than the `field_setting`
26
+ # as the fieldable_id is related to the `component`, `board` or `repeater` rather than `field_setting`
27
27
  has_many :texts, dependent: :delete_all
28
+ has_many :strings, dependent: :delete_all
28
29
  has_many :dates, dependent: :delete_all
29
30
  has_many :galleries, dependent: :delete_all
30
31
  has_many :repeater, dependent: :delete_all
31
32
  has_many :radio, dependent: :delete_all
32
- has_many :select, dependent: :delete_all
33
+ has_many :selection, dependent: :delete_all
33
34
  has_many :checkbox, dependent: :delete_all
34
35
 
35
36
  has_many :choices, dependent: :delete_all
36
- has_one :default_choice, class_name: 'Binda::Choice', dependent: :delete
37
+ has_one :default_choice, -> (field_setting) { where(id: field_setting.default_choice_id) }, class_name: 'Binda::Choice'
37
38
 
38
39
  accepts_nested_attributes_for :choices, allow_destroy: true, reject_if: :is_rejected
39
40
 
41
+
42
+ #
43
+ # Sets the validation rules to accept and save an attribute
40
44
  def is_rejected( attributes )
41
45
  attributes['label'].blank? || attributes['content'].blank?
42
46
  end
43
47
 
44
48
  cattr_accessor :field_settings_array
45
49
 
50
+ after_create :set_allow_null
51
+
46
52
  after_create do
47
53
  self.class.reset_field_settings_array
48
54
  end
@@ -51,45 +57,42 @@ module Binda
51
57
  self.class.reset_field_settings_array
52
58
  end
53
59
 
54
- def self.get_fieldables
55
- # TODO add 'Gallery' to this list
56
- %w( Text Date Asset Repeater Radio Select Checkbox )
57
- end
58
-
59
- # Field types are't fieldable! watch out! They might use the same model (eg `string` and `text`)
60
- def get_field_types
61
- # TODO add 'gallery' to this list
62
- %w( string text asset repeater date radio select checkbox )
60
+ def self.get_field_classes
61
+ %w( String Text Date Asset Repeater Radio Selection Checkbox )
63
62
  end
64
63
 
65
64
  # Validations
66
65
  validates :name, presence: true
67
- # validates :field_type, presence: true, inclusion: { in: :get_field_types }
66
+ validates :field_type, presence: true
67
+ validates :field_type, inclusion: { in: [ *FieldSetting.get_field_classes.map{ |fc| fc.to_s.underscore }, '' ], message: "Field type must be one of the following #{ FieldSetting.get_field_classes.join(", ") }" }
68
68
  validates :field_group_id, presence: true
69
69
 
70
70
  # Slug
71
71
  extend FriendlyId
72
72
  friendly_id :default_slug, use: [:slugged, :finders]
73
73
 
74
- # CUSTOM METHODS
75
- #
76
- # @see https://github.com/norman/friendly_id/issues/436
77
74
 
78
- def should_generate_new_friendly_id?
79
- slug.blank?
80
- end
75
+ # Friendly id preference on slug generation
76
+ #
77
+ # Method inherited from friendly id
78
+ # @see https://github.com/norman/friendly_id/issues/436
79
+ def should_generate_new_friendly_id?
80
+ slug.blank?
81
+ end
81
82
 
83
+ # Set slug name
84
+ #
85
+ # It generates 4 possible slugs before falling back to FriendlyId default behaviour
82
86
  def default_slug
83
- slug = self.field_group.structure.name
84
-
87
+ slug = ''
88
+ slug << self.field_group.structure.name
85
89
  slug << '-'
86
90
  slug << self.field_group.name
87
-
88
91
  unless self.parent.nil?
89
92
  slug << '-'
90
93
  slug << self.parent.name
91
94
  end
92
-
95
+
93
96
  possible_names = [
94
97
  "#{ slug }--#{ self.name }",
95
98
  "#{ slug }--#{ self.name }-1",
@@ -102,14 +105,16 @@ module Binda
102
105
 
103
106
  # Retrieve the ID if a slug is provided and update the field_settings_array
104
107
  # in order to avoid calling the database (or the cached response) every time.
105
- # This way Rails logs are much cleaner
108
+ # This should speed up requests and make Rails logs are cleaner.
106
109
  #
107
110
  # @return [integer] The ID of the field setting
108
111
  def self.get_id( field_slug )
109
112
  # Get field setting id from slug, without multiple calls to database
110
113
  # (the query runs once and caches the result, then any further call uses the cached result)
111
- @@field_settings_array = self.all if @@field_settings_array.nil?
112
- @@field_settings_array.find { |fs| fs.slug == field_slug }.id
114
+ @@field_settings_array = self.pluck(:slug, :id) if @@field_settings_array.nil?
115
+ id = @@field_settings_array.select{ |fs| fs[0] == field_slug }[0][1]
116
+ raise ArgumentError, "There isn't any field setting with the current slug.", caller if id.nil?
117
+ return id
113
118
  end
114
119
 
115
120
  # Reset the field_settings_array. It's called every time
@@ -123,5 +128,11 @@ module Binda
123
128
  @@field_settings_array = nil
124
129
  end
125
130
 
131
+ # Make sure that allow_null is set to false instead of nil.
132
+ # This isn't done with a database constraint in order to gain flexibility
133
+ def set_allow_null
134
+ self.allow_null = false if self.allow_null.nil?
135
+ end
136
+
126
137
  end
127
138
  end
@@ -6,13 +6,6 @@ module Binda
6
6
  has_many :assets, through: :bindings
7
7
  belongs_to :fieldable, polymorphic: true
8
8
  belongs_to :field_setting
9
-
10
- # Validations
11
-
12
- # Slug
13
-
14
- # CUSTOM METHODS
15
- # --------------
16
9
 
17
10
  end
18
11
  end
@@ -1,5 +1,5 @@
1
1
  module Binda
2
- class Radio < Select
2
+ class Radio < Selection
3
3
 
4
4
  end
5
5
  end