Skip to content

Commit

Permalink
RequestHandler options from root are merged no more {}, {foo: "bar"}.…
Browse files Browse the repository at this point in the history
… RequestHandler option namespace: String is

added for prefixing route's URL. default Model self.responses_on_edit is added.
A bit refactoring for demo is made.
  • Loading branch information
classyPimp committed Dec 18, 2015
1 parent 00dfed6 commit 064bf29
Show file tree
Hide file tree
Showing 23 changed files with 307 additions and 118 deletions.
46 changes: 46 additions & 0 deletions app/assets/javascripts/components/admin/dashboards/general.js.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Components
module Admin
module Dashboards
class General < RW

expose

def get_initial_state
@blank_control_component = ->{Native(t(:div, {}))}
{
current_control_component: @blank_control_component
}
end

def render
t(:div, {},
t(:div, {className: "row"},
t(:button, {onClick: ->{init_user_creation}}, "add users"),
t(:button, {onClick: ->{init_users_index} }, "list users")
),
t(:div, {className: "row"},
t(:div, {className: "container"},
state.current_control_component.to_n
)
)
)
end

def init_user_creation
set_state current_control_component: Native(t(Components::Users::New, {on_create: ->(user){on_user_added(user)}, as_admin: true}))
end

def init_users_index
set_state current_control_component: Native(t(Components::Users::Index, {as_admin: true}))
end

def on_user_added(user)
msg = Shared::Flash::Message.new(t(:div, {}, link_to("user created press here to show", "/users/show/#{user.id}")), "success")
Components::App::Main.instance.ref(:flash).rb.add_message(msg)
set_state current_control_component: @blank_control_component
end

end
end
end
end
17 changes: 17 additions & 0 deletions app/assets/javascripts/components/admin/dashboards/main.js.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Components
module Admin
module Dashboards
class Main < RW

expose

def render
t(:div, {},
children
)
end

end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Components
module Dashboards
module Admin
class Main < RW

expose

def render
Expand All @@ -12,4 +11,4 @@ def render

end
end
end
end
16 changes: 16 additions & 0 deletions app/assets/javascripts/components/admin/users/user_edit.js.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Components
module Admin
module Users
class Edit < RW
expose

def render
t(:div, {},
t(Components::Users::Edit, {as_admin: true, params: props.params})
)
end

end
end
end
end
7 changes: 5 additions & 2 deletions app/assets/javascripts/components/app/router.js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ def render
t(`Route`, {path: "new", component: Components::Images::Create.create_class})
),

t(`Route`, {path: "/dashboards", component: Components::Dashboards::Main.create_class, onEnter: ->(n, r, cb){check_role(n, r, cb, [:admin])}},
t(`Route`, {path: "admin", component: Components::Dashboards::Admin.create_class})
t(`Route`, {path: "/admin", component: Components::Admin::Main.create_class, onEnter: ->(n, r, cb){check_role(n, r, cb, [:admin])}},
t(`Route`, {path: "dashboards", component: Components::Admin::Dashboards::Main.create_class},
t(`Route`, {path: "general", component: Components::Admin::Dashboards::General.create_class})
),
t(`Route`, {path: "users/:id/edit", component: Components::Admin::Users::Edit.create_class})
),

t(`Route`, {path: "menues/index_edit", component: Components::Menues::IndexEdit.create_class}),
Expand Down
44 changes: 0 additions & 44 deletions app/assets/javascripts/components/dashboards/admin.js.rb

This file was deleted.

7 changes: 5 additions & 2 deletions app/assets/javascripts/components/forms/select.js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def render
t(:p, {},
*splat_each(state.selected) do |selected|
t(:span, {className: "label label-default", style: {cursor: "pointer"}, onClick: ->{delete(selected)}},
if @serialize_value
if @serialize_value
next if selected.attributes[:_destroy]
"#{selected.attributes[props.serialize_value[:value_attr]]} X"
else
"#{selected} X"
Expand Down Expand Up @@ -117,9 +118,11 @@ def delete(selected)
if @serialize_value
if selected.arbitrary[:initially_selected]
selected.attributes[:_destroy] = "1"
state.options << selected
end
else
state.options << state.selected.delete(selected)
end
state.options << state.selected.delete(selected)
set_state options: state.options
end

Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/components/pages/edit.js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get_initial_state
end

def component_did_mount
Page.show({id: props.params.id}).then do |page|
Page.show(wilds: {id: props.params.id}).then do |page|
set_state form_model: page
end.fail do |res|
alert res
Expand Down
4 changes: 2 additions & 2 deletions app/assets/javascripts/components/pages/index.js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get_initial_state
end

def component_did_mount
Page.index({component: self}, {extra_params: {per_page: 1}}).then do |pages|
Page.index(component: self, extra_params: {per_page: 1}).then do |pages|
extract_pagination(pages)
#p pages.pure_attributes
set_state pages: pages
Expand Down Expand Up @@ -44,7 +44,7 @@ def render
end

def pagination_switch_page(_page, per_page)
Page.index({},{extra_params: {page: _page, per_page: per_page}}).then do |pages|
Page.index(extra_params: {page: _page, per_page: per_page}).then do |pages|
extract_pagination(pages)
set_state pages: pages
end
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/components/pages/show.js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def get_initial_state

def component_did_mount
page_to_query = (x = props.page_id) ? x : props.params.id
Page.show({id: page_to_query, component: self}).then do |page|
Page.show(wilds: {id: page_to_query}, component: self).then do |page|
set_state page: page
end.fail do |resp|
raise resp
Expand Down
10 changes: 6 additions & 4 deletions app/assets/javascripts/components/users/edit.js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ def get_initial_state

def component_did_mount
id = props.params.id
unless CurrentUser.user_instance.id == id.to_i || CurrentUser.user_instance.has_role?(:admin)
@as_admin = props.as_admin ? {namespace: "admin"} : {}
unless CurrentUser.user_instance.id == id.to_i || props.as_admin
props.history.replaceState({}, "/forbidden")
else
User.show({id: id}).then do |form_model|
User.edit({wilds: {id: id}}.merge(@as_admin)).then do |form_model|
set_state form_model: form_model
end
end
Expand All @@ -36,7 +37,7 @@ def render
t(:p, {}, "email: #{state.form_model.email}"),
t(:button, {onClick: ->{init_auth_data_edit}}, "edit login credentials"),
input(Forms::Input, state.form_model.profile, :bio),
if CurrentUser.user_instance.has_role?(:admin)
if props.as_admin
input(Forms::Select, state.form_model, :roles, { serialize_value: {model_name: "role", value_attr: "name"},
multiple: true, server_feed: {url: "/api/users/roles_feed"} })
end,
Expand All @@ -49,8 +50,9 @@ def render

def handle_inputs
collect_inputs
p state.form_model.pure_attributes
unless state.form_model.has_errors?
state.form_model.update({}, {serialize_as_form: true}).then do |model|
state.form_model.update({serialize_as_form: true}.merge(@as_admin)).then do |model|
unless model.has_errors?
msg = Shared::Flash::Message.new(t(:div, {}, "updated successfully"))
Components::App::Main.instance.ref(:flash).rb.add_message(msg)
Expand Down
6 changes: 4 additions & 2 deletions app/assets/javascripts/components/users/index.js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def get_initial_state
end

def component_did_mount
User.index({}, {extra_params: {per_page: 25}}).then do |users|
@as_admin = props.as_admin ? {namespace: "admin"} : {}
User.index({extra_params: {per_page: 25}}.merge(@as_admin)).then do |users|
extract_pagination(users)
set_state users: users
end
Expand Down Expand Up @@ -55,7 +56,8 @@ def render
end

def edit_selected(user)
Components::App::Router.history.replaceState({}, "/users/edit/#{user.id}")
url = props.as_admin ? "/admin/users/#{user.id}/edit" : "/users/edit/#{user.id}"
Components::App::Router.history.pushState({}, url)
end

def destroy_selected(_user)
Expand Down
7 changes: 4 additions & 3 deletions app/assets/javascripts/components/users/new.js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class New < RW

def prepare_new_user
->{
User.new(profile: {profile: {}}, avatar: {avatar: {}})
User.new(profile: {profile: {}}, avatar: {avatar: {}}, roles: [])
}
end

Expand All @@ -33,7 +33,8 @@ def render
if state.current_user.has_role? :admin
t(:div, {},
t(:p, {}, "choose roles"),
input(Forms::Select, state.form_model, :roles_array, {multiple: true, server_feed: {url: "/api/users/roles_feed"}})
input(Forms::Select, state.form_model, :roles, {multiple: true, server_feed: {url: "/api/users/roles_feed"},
serialize_value: {model_name: "role", value_attr: "name"}})
)
end,
t(:br, {}),
Expand All @@ -46,7 +47,7 @@ def render
def handle_inputs
collect_inputs
unless state.form_model.has_errors?
state.form_model.create({}, {serialize_as_form: true}).then do |model|
state.form_model.create(serialize_as_form: true, namespace: "admin").then do |model|
if model.has_errors?
set_state form_model: model
else
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/components/users/show.js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def get_initial_state
end

def component_did_mount
User.show({id: props.params.id}).then do |user|
User.show(wilds: {id: props.params.id}).then do |user|
set_state user: user
end.fail do |response|
`console.log("error:")`
Expand Down
8 changes: 4 additions & 4 deletions app/assets/javascripts/controllers/users_controller.js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class UsersController < BaseController
def handle_signup_submit
c.collect_inputs
unless c.state.form_model.has_errors?
c.state.form_model.sign_up({yield_response: true}, payload: {user: c.state.form_model.attributes}).then do |response|
c.state.form_model.sign_up(yield_response: true, payload: {user: c.state.form_model.attributes}).then do |response|
if e = response.json[:user][:errors]
c.state.form_model.errors = e
c.set_state form_model: c.state.form_model
Expand All @@ -23,7 +23,7 @@ def handle_signup_submit
def login
c.collect_inputs(validate_only: [nil])
unless c.state.form_model.has_errors?
CurrentUser.login({}, payload: {session: c.state.form_model.attributes}).then do |response|
CurrentUser.login(payload: {session: c.state.form_model.attributes}).then do |response|
if x = response[:errors]
c.set_state message: x
else
Expand All @@ -38,7 +38,7 @@ def login
def send_password_reset_email
c.collect_inputs
unless c.state.form_model.has_errors?
CurrentUser.request_password_reset({}, payload: {password_reset: c.state.form_model.attributes}).then do |response|
CurrentUser.request_password_reset(payload: {password_reset: c.state.form_model.attributes}).then do |response|
c.set_state message: "instructions were sent to you"
end.fail do |response|
c.state.form_model.errors = response[:errors]
Expand All @@ -52,7 +52,7 @@ def send_password_reset_email
def update_new_password
c.collect_inputs(validate_only: [:password, :password_confirmation])
unless c.state.form_model.has_errors?
CurrentUser.update_new_password({id: c.state.id}, payload: {user: c.state.form_model.attributes, email: c.state.email}).then do |response|
CurrentUser.update_new_password(wilds: {id: c.state.id}, payload: {user: c.state.form_model.attributes, email: c.state.email}).then do |response|
Components::App::Router.history.pushState(nil, "/users/show/#{CurrentUser.user_instance.id}")
end.fail do |response|
c.set_state message: "error"
Expand Down
2 changes: 2 additions & 0 deletions app/assets/javascripts/front_models/user.js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class User < Model
route "Index", get: "users"

route "destroy", {delete: "users/:id"}, {defaults: [:id]}

route "Edit", {get: "users/:id/edit"}

has_one :profile, :avatar
has_many :roles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module DependsOnCurrentUser

def component_will_mount
state.current_user = User.new
CurrentUser.get_current_user({}, {extra_params: {roles: self.class.roles_to_fetch}}).then do |user|
CurrentUser.get_current_user(extra_params: {roles: self.class.roles_to_fetch}).then do |user|
if user.is_a? User
depends_on_current_user_loaded(user) if self.respond_to? :user_loaded
CurrentUser.user_instance = user
Expand Down
Loading

0 comments on commit 064bf29

Please sign in to comment.