Class: Avo::Resources::Resource
- Inherits:
-
Object
- Object
- Avo::Resources::Resource
- Defined in:
- lib/avo/app/resource.rb,
lib/avo/app/resource_fields.rb,
lib/avo/app/resource_actions.rb,
lib/avo/app/resource_filters.rb,
lib/avo/app/resource_grid_fields.rb
Constant Summary collapse
- @@fields =
{}
- @@actions =
{}
- @@filters =
{}
- @@grid_fields =
{}
Instance Attribute Summary collapse
-
#default_view_type ⇒ Object
readonly
Returns the value of attribute default_view_type.
-
#includes ⇒ Object
readonly
Returns the value of attribute includes.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
Class Method Summary collapse
- .add_field(resource, field) ⇒ Object
- .body(name, **args, &block) ⇒ Object
- .fields(&block) ⇒ Object
- .get_actions ⇒ Object
- .get_fields ⇒ Object
- .get_filters ⇒ Object
- .get_grid_fields ⇒ Object
- .grid(&block) ⇒ Object
- .hydrate_resource(model, resource, view = :index) ⇒ Object
- .index_path(resource_or_class) ⇒ Object
- .preview(name, **args, &block) ⇒ Object
- .show_path(resource) ⇒ Object
- .title(name, **args, &block) ⇒ Object
- .trix(name, **args, &block) ⇒ Object
- .use_action(action) ⇒ Object
- .use_filter(filter) ⇒ Object
Instance Method Summary collapse
- #attached_file_fields ⇒ Object
- #available_view_types ⇒ Object
- #fill_model(model, params) ⇒ Object
- #get_actions ⇒ Object
- #get_fields ⇒ Object
- #get_filters ⇒ Object
- #get_grid_fields ⇒ Object
- #has_devise_password ⇒ Object
- #model ⇒ Object
- #name ⇒ Object
- #query_search(query: '', via_resource_name:, via_resource_id:) ⇒ Object
- #resource_name_plural ⇒ Object
- #resource_name_singular ⇒ Object
- #search ⇒ Object
- #title ⇒ Object
- #underscore_name ⇒ Object
- #url ⇒ Object
Instance Attribute Details
#default_view_type ⇒ Object (readonly)
Returns the value of attribute default_view_type.
12 13 14 |
# File 'lib/avo/app/resource.rb', line 12 def default_view_type @default_view_type end |
#includes ⇒ Object (readonly)
Returns the value of attribute includes.
11 12 13 |
# File 'lib/avo/app/resource.rb', line 11 def includes @includes end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
10 11 12 |
# File 'lib/avo/app/resource.rb', line 10 def required @required end |
Class Method Details
.add_field(resource, field) ⇒ Object
16 17 18 |
# File 'lib/avo/app/resource_fields.rb', line 16 def add_field(resource, field) @@fields[resource].push field end |
.body(name, **args, &block) ⇒ Object
24 25 26 |
# File 'lib/avo/app/resource_grid_fields.rb', line 24 def body(name, **args, &block) @@grid_fields[self][:body] = Avo::GridFields::BodyField::new(name, **args, &block) end |
.fields(&block) ⇒ Object
7 8 9 10 |
# File 'lib/avo/app/resource_fields.rb', line 7 def fields(&block) @@fields[self] ||= [] yield end |
.get_actions ⇒ Object
12 13 14 |
# File 'lib/avo/app/resource_actions.rb', line 12 def get_actions @@actions[self] or [] end |
.get_fields ⇒ Object
12 13 14 |
# File 'lib/avo/app/resource_fields.rb', line 12 def get_fields @@fields[self] or [] end |
.get_filters ⇒ Object
12 13 14 |
# File 'lib/avo/app/resource_filters.rb', line 12 def get_filters @@filters[self] or [] end |
.get_grid_fields ⇒ Object
12 13 14 |
# File 'lib/avo/app/resource_grid_fields.rb', line 12 def get_grid_fields @@grid_fields[self] or {} end |
.grid(&block) ⇒ Object
7 8 9 10 |
# File 'lib/avo/app/resource_grid_fields.rb', line 7 def grid(&block) @@grid_fields[self] ||= {} yield end |
.hydrate_resource(model, resource, view = :index) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/avo/app/resource.rb', line 15 def hydrate_resource(model, resource, view = :index) default_panel_name = "#{resource.name} details" resource_with_fields = { id: model.id, resource_name_singular: resource.resource_name_singular, resource_name_plural: resource.resource_name_plural, title: model[resource.title], fields: [], grid_fields: {}, panels: [{ name: default_panel_name, component: 'panel', }] } grid_fields = resource.get_grid_fields grid_fields_by_required_field = grid_fields.map { |grid_field_id, field| [field.id, grid_field_id] }.to_h resource.get_fields.each do |field| field_is_required_in_grid_view = grid_fields.map { |grid_field_id, field| field.id }.include?(field.id) required_in_current_view = field.send("show_on_#{view.to_s}") next unless required_in_current_view or field_is_required_in_grid_view furnished_field = field.fetch_for_resource(model, resource, view) next if furnished_field.blank? furnished_field[:panel_name] = default_panel_name furnished_field[:show_on_show] = field.show_on_show if field.has_own_panel? furnished_field[:panel_name] = field.name.to_s.pluralize end if field_is_required_in_grid_view required_field = grid_fields_by_required_field[field.id] resource_with_fields[:grid_fields][required_field] = furnished_field end if required_in_current_view resource_with_fields[:fields] << furnished_field end end resource_with_fields end |
.index_path(resource_or_class) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/avo/app/resource.rb', line 68 def index_path(resource_or_class) if resource_or_class.class == String url = resource_or_class.underscore.pluralize else url = resource_or_class.class.name.demodulize.underscore.pluralize end "/resources/#{url}" end |
.preview(name, **args, &block) ⇒ Object
16 17 18 |
# File 'lib/avo/app/resource_grid_fields.rb', line 16 def preview(name, **args, &block) @@grid_fields[self][:preview] = Avo::GridFields::PreviewField::new(name, **args, &block) end |
.show_path(resource) ⇒ Object
64 65 66 |
# File 'lib/avo/app/resource.rb', line 64 def show_path(resource) "/resources/#{resource.class.model_name.plural}/#{resource.id}" end |
.title(name, **args, &block) ⇒ Object
20 21 22 |
# File 'lib/avo/app/resource_grid_fields.rb', line 20 def title(name, **args, &block) @@grid_fields[self][:title] = Avo::GridFields::TitleField::new(name, **args, &block) end |
.trix(name, **args, &block) ⇒ Object
20 21 22 |
# File 'lib/avo/app/resource_fields.rb', line 20 def trix(name, **args, &block) @@fields[self].push Avo::Fields::TrixField::new(name, **args, &block) end |
.use_action(action) ⇒ Object
7 8 9 10 |
# File 'lib/avo/app/resource_actions.rb', line 7 def use_action(action) @@actions[self] ||= [] @@actions[self].push(action) end |
.use_filter(filter) ⇒ Object
7 8 9 10 |
# File 'lib/avo/app/resource_filters.rb', line 7 def use_filter(filter) @@filters[self] ||= [] @@filters[self].push(filter) end |
Instance Method Details
#attached_file_fields ⇒ Object
169 170 171 172 173 |
# File 'lib/avo/app/resource.rb', line 169 def attached_file_fields get_fields.select do |field| [Avo::Fields::FileField, Avo::Fields::FilesField].include? field.class end end |
#available_view_types ⇒ Object
125 126 127 |
# File 'lib/avo/app/resource.rb', line 125 def available_view_types get_grid_fields.length > 0 ? [:grid, :table] : [:table] end |
#fill_model(model, params) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/avo/app/resource.rb', line 179 def fill_model(model, params) # Map the received params to their actual fields fields_by_database_id = self.get_fields.map { |field| [field.database_id(model).to_s, field] }.to_h params.each do |key, value| field = fields_by_database_id[key] next unless field model = field.fill_field model, key, value end model end |
#get_actions ⇒ Object
133 134 135 |
# File 'lib/avo/app/resource.rb', line 133 def get_actions self.class.get_actions end |
#get_fields ⇒ Object
117 118 119 |
# File 'lib/avo/app/resource.rb', line 117 def get_fields self.class.get_fields end |
#get_filters ⇒ Object
129 130 131 |
# File 'lib/avo/app/resource.rb', line 129 def get_filters self.class.get_filters end |
#get_grid_fields ⇒ Object
121 122 123 |
# File 'lib/avo/app/resource.rb', line 121 def get_grid_fields self.class.get_grid_fields end |
#has_devise_password ⇒ Object
175 176 177 |
# File 'lib/avo/app/resource.rb', line 175 def has_devise_password @has_devise_password or false end |
#model ⇒ Object
111 112 113 114 115 |
# File 'lib/avo/app/resource.rb', line 111 def model return @model if @model.present? underscore_name.to_s.camelize.singularize end |
#name ⇒ Object
79 80 81 82 83 |
# File 'lib/avo/app/resource.rb', line 79 def name return @name if @name.present? self.class.name.demodulize.titlecase end |
#query_search(query: '', via_resource_name:, via_resource_id:) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/avo/app/resource.rb', line 141 def query_search(query: '', via_resource_name: , via_resource_id:) db_query = self.model if via_resource_name.present? = App.get_resource_by_name(via_resource_name) = .model db_query = .find(via_resource_id).public_send(self.resource_name_plural.downcase) end [self.search].flatten.each_with_index do |search_by, index| query_string = "text(#{search_by}) ILIKE '%#{query}%'" if index == 0 db_query = db_query.where query_string else db_query = db_query.or(self.model.where query_string) end end db_query.select("#{:id}, #{title} as \"name\"") end |
#resource_name_plural ⇒ Object
89 90 91 |
# File 'lib/avo/app/resource.rb', line 89 def resource_name_plural name.pluralize end |
#resource_name_singular ⇒ Object
85 86 87 |
# File 'lib/avo/app/resource.rb', line 85 def resource_name_singular name end |
#search ⇒ Object
137 138 139 |
# File 'lib/avo/app/resource.rb', line 137 def search @search end |
#title ⇒ Object
105 106 107 108 109 |
# File 'lib/avo/app/resource.rb', line 105 def title return @title if @title.present? 'id' end |
#underscore_name ⇒ Object
93 94 95 96 97 |
# File 'lib/avo/app/resource.rb', line 93 def underscore_name return @name if @name.present? self.class.name.demodulize.underscore end |
#url ⇒ Object
99 100 101 102 103 |
# File 'lib/avo/app/resource.rb', line 99 def url return @url if @url.present? self.class.name.demodulize.underscore.pluralize end |